簡體   English   中英

在 PHP 中使用 CURL POST JSON Body

[英]POST JSON Body with CURL in PHP

我正在運行如下代碼

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://chat-service.com/api/direct',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_SSL_VERIFYHOST => 0,
  CURLOPT_SSL_VERIFYPEER => 0,
  CURLOPT_POSTFIELDS =>'{
  "to_number": "62111111111",
  "to_name": "Mr X",
  "message_template_id": "abc123abc123",
  "channel_integration_id": "123abc123abc",
  "language": {
    "code": "id"
  },
  "parameters": {
    "body": [
      {
        "key": "1",
        "value": "name",
        "value_text": "Mr X"
      },
      {
        "key": "2",
        "value": "vehiclereg",
        "value_text": "L0001X"
      },
      {
        "key": "3",
        "value": "date",
        "value_text": "29 Feb 2022"
      }
    ]
  }
}

',
  CURLOPT_HTTPHEADER => array(
    'Content-Type: application/json',
    'Authorization: Bearer 1122334455',
    'target_channel: trial'
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;
?>

現在我想將 CURLOPT_POSTFIELDS 值存儲為變量,這是我的代碼,但仍未運行

<?php

$curl = curl_init();
$data = array
    (
    "to_number"=>'62111111111',
    "to_name"=>'Mr X',
    "message_template_id"=>'abc123abc123',
    "channel_integration_id"=>'123abc123abc',
    "language"=>
    array 
        (
            "code"=>"id"
        ),
    "parameters"=>
    array
        (
            "body"=>array 
            (
                "key"=>"1",
                "value"=>"name",
                "value_text"=>"Mr X"
            ),
            "body"=>array 
            (
                "key"=>"2",
                "value"=>"vehiclereg",
                "value_text"=>"L0001X"
            ),
            "body"=>array 
            (
                "key"=>"3",
                "value"=>"date",
                "value_text"=>"29 Feb 2022"
            )
        )
    );
curl_setopt_array($curl, array(
CURLOPT_HTTPHEADER => array(
    'Content-Type: application/json',
    'Authorization: Bearer 1122334455',
    'target_channel: trial'
    ),
CURLOPT_URL => 'https://chat-service.com/api/direct',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_POSTFIELDS => http_build_query($data);
));

$response = curl_exec($curl);
curl_close($curl);

echo "<pre>";
print_r($response)

?>

在我看來,這一行是懷疑為什么代碼不起作用但仍然不知道如何修復它

運行一個

  "parameters": {
    "body": [
      {
        "key": "1",
        "value": "name",
        "value_text": "Mr X"
      },
      {
        "key": "2",
        "value": "vehiclereg",
        "value_text": "L0001X"
      },
      {
        "key": "3",
        "value": "date",
        "value_text": "29 Feb 2022"
      }
    ]
  }

未運行

"parameters"=>
    array
        (
            "body"=>array 
            (
                "key"=>"1",
                "value"=>"name",
                "value_text"=>"Mr X"
            ),
            "body"=>array 
            (
                "key"=>"2",
                "value"=>"vehiclereg",
                "value_text"=>"L0001X"
            ),
            "body"=>array 
            (
                "key"=>"3",
                "value"=>"date",
                "value_text"=>"29 Feb 2022"
            )
        )

我嘗試在任何地方使用谷歌搜索它找到一些類似的情況,比如在這個發送 post json with php (curl)但仍然可以修復它

我確實是這樣編碼的

<?php
$data = array
    (
    "to_number"=>'62111111111',
    "to_name"=>'Mr X',
    "message_template_id"=>'abc123abc123',
    "channel_integration_id"=>'123abc123abc',
    "language"=>
        array 
        (
            "code"=>"id"
        ),
    "parameters"=>
        array
        (
            "body"=>array 
                (
                "key"=>"1",
                "value"=>"name",
                "value_text"=>"Mr X"
                ),
             "body"=>array 
                (
                "key"=>"2",
                "value"=>"vehiclereg",
                "value_text"=>"L0001X"
                ),
             "body"=>array 
                (
                "key"=>"3",
                "value"=>"date",
                "value_text"=>"29 Feb 2022"
                )
        )
    );

echo json_encode($data);
?>

這就是結果

{
  "to_number": "62111111111",
  "to_name": "Mr X",
  "message_template_id": "abc123abc123",
  "channel_integration_id": "123abc123abc",
  "language": {
    "code": "id"
  },
  "parameters": {
    "body": {
      "key": "3",
      "value": "date",
      "value_text": "29 Feb 2022"
    }
  }
}

嘗試使用

CURLOPT_POSTFIELDS => json_encode($data);

參考如何使用 PHP cURL POST JSON 數據?

終於找到了基於試錯和到處谷歌搜索的解決方案

<?php

$curl = curl_init();
$data = 
[
    "to_number"=>'62111111111',
    "to_name"=>'Mr X',
    "message_template_id"=>'abc123abc123',
    "channel_integration_id"=>'123abc123abc',
    "language"=> 
    [
        "code"=>"id"
    ],
    "parameters"=>
    [
        "body" =>
        [ 
            [
                "key"=>"1",
                "value"=>"name",
                "value_text"=>"Mr X"
            ],
            [
                "key"=>"2",
                "value"=>"vehiclereg",
                "value_text"=>"L0001X"
            ],
            [
                "key"=>"3",
                "value"=>"date",
                "value_text"=>"29 Februari 2022"
            ]
        ]
    ]
];
curl_setopt_array($curl, array(
CURLOPT_HTTPHEADER => array(
    'Content-Type: application/json',
    'Authorization: Bearer 1122334455',
    'target_channel: tria;'
    ),
CURLOPT_URL => 'https://chat-service.com/api/direct',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_POSTFIELDS => json_encode($data)
));

$response = curl_exec($curl);
curl_close($curl);

echo "<pre>";
print_r($response);
?>

感謝堆棧溢出的啟發

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM