【问题标题】:How to pass raw body parameters in post api [closed]如何在post api中传递原始身体参数[关闭]
【发布时间】:2020-05-09 09:42:36
【问题描述】:

我正在使用 Delhivery API,我在邮递员中使用它,它在那里工作。 这是 post 方法 api,我以原始形式传递我的值。 传递数据的格式如下截图 This is the format to pass variable in post method

This i passed in postman and its working here

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

【问题讨论】:

标签: php json laravel api


【解决方案1】:

您可以使用 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;
}

【讨论】:

  • Q.4- 在包订单创建 API 响应中出现错误“帖子中缺少格式密钥”? Ans- "format=json&data="(不带引号)是强制传递请求有效负载以便在我们的系统中创建订单。我收到此错误告诉我如何以“format=json&data="这种格式 传递数据
  • 您必须发送从“format=json&data=" 开始的相同数据结构,如此处提到的imgur.com/zvdNil9
【解决方案2】:

我正在这样做,但对我不起作用,出现“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);

【讨论】:

    猜你喜欢
    • 2016-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-05
    • 1970-01-01
    • 1970-01-01
    • 2011-10-26
    • 1970-01-01
    相关资源
    最近更新 更多