Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在post api中传递原始主体参数_Php_Json_Laravel_Api - Fatal编程技术网

Php 如何在post api中传递原始主体参数

Php 如何在post api中传递原始主体参数,php,json,laravel,api,Php,Json,Laravel,Api,我正在使用Delhivery API,我在postman中找到了它,它在那里工作。 这是post方法api,我正在用raw传递我的值。 传递数据的格式如下面的屏幕截图所示 请告诉我如何在laravel PHP post方法中以相同的格式传递这些数据。并传递身份验证头。您可以使用PHP CURL扩展 <?php $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => "https://stagi

我正在使用Delhivery API,我在postman中找到了它,它在那里工作。 这是post方法api,我正在用raw传递我的值。 传递数据的格式如下面的屏幕截图所示


请告诉我如何在laravel PHP post方法中以相同的格式传递这些数据。并传递身份验证头。

您可以使用PHP CURL扩展

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://staging-express.delhivery.com/api/cmu/create.json",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "Body goes here",
  CURLOPT_HTTPHEADER => array(
    "cache-control: no-cache",
    "content-type: application/x-www-form-urlencoded"
    'x-api-key: XXXXXX',
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

我是这样做的,但不适合我,geting“format=json&data=”error


您可以在Bear Token中通过标头中的身份验证我正在这样做检查此屏幕快照您在狂饮它是内置的laravell Q.4-在包订单创建API响应中获取错误“后文中缺少格式密钥”?Ans-“format=json&data=”(不带引号)必须传入请求有效负载,以便在我们的系统中创建订单。我收到此错误。请告诉我如何以“format=json&data=”此格式传递数据。您必须从“format=json&data=”开始发送相同的数据结构,如下所述
    $client = new Client();

    $data['shipments'][] = [
        'add'=> 'f425 bsquare mohali',
        'phone'=> '9888429895',
        'payment_mode'=> 'Prepaid',
        'name'=> 'Lalit Mohan',
        'pin'=> '145001',
        'order'=> 'RH5E171FCFDEEBA',
        'seller_gst_tin'=> '06AAMCA5258P1Z1',
        'gst_cess_amount'=> '100',
        'client'=> 'Lalit Mohan',
        'tax_value'=> '100',
        'city'=> 'Mohali',
        'weight'=> '100',
        'product_quantity'=> '2',
        'state'=> 'Punjab',
        'waybill'=> '4468910000173',
        'order_date'=> '20170520',
        'total_amount'=> '21840',
    ];
    $data['shipments'] = json_encode($data['shipments']);
    $data['pickup_location'] = [
        'name'=> 'Randh Panchkula',
        'city'=> 'Panchkula',
        'pin'=> '160101',
        'country'=> 'India',
        'phone'=> '9888429895',
        'add'=> 'randh panchkula Address'
    ];
    $data['pickup_location'] = json_encode($data['pickup_location']);

    $client = new Client();


    $response = $client->request('POST', 'https://staging-express.delhivery.com/api/cmu/create.json', [
            'headers' => [
                'authorization' => 'Token XXXXX',
                'contentType'=> 'application/json'
            ],
            'form_params' => [
                'format' => json_encode($data),
            ],
        ]);
    $response_data = json_decode($response->getBody()->getContents());
    dd($response_data);