简体   繁体   English

在 PHP 中使用 CURL POST JSON Body

[英]POST JSON Body with CURL in PHP

I have running code like the following我正在运行如下代码

<?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;
?>

now i'd like to store CURLOPT_POSTFIELDS value as variable and this is my code but stil not running现在我想将 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)

?>

In my opininion this line is the suspect why the code not working but still don't know how to fix it在我看来,这一行是怀疑为什么代码不起作用但仍然不知道如何修复它

Running one运行一个

  "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"
      }
    ]
  }

Not Running未运行

"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"
            )
        )

I tried googling it everywhere find some similar case like on this one send post json with php (curl) but still can fix it我尝试在任何地方使用谷歌搜索它找到一些类似的情况,比如在这个发送 post json with php (curl)但仍然可以修复它

i did encode like this我确实是这样编码的

<?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);
?>

and this is the result这就是结果

{
  "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"
    }
  }
}

Try use尝试使用

CURLOPT_POSTFIELDS => json_encode($data);

Reference How to POST JSON Data With PHP cURL?参考如何使用 PHP cURL POST JSON 数据?

Finally found the solution based on trial error and googling everywhere终于找到了基于试错和到处谷歌搜索的解决方案

<?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);
?>

thank you stack overflow for inspiring感谢堆栈溢出的启发

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM