在PHP中用JSON发送键值对的正确方法是什么

时间:2015-07-22 05:24:47

标签: php json post

我正在尝试用PHP进行POST。在文件中,他们提到了以下格式。

{
"EmailAddress": "john.smith@acmeconsulting.co",
"ActivityEvent": 112,
"ActivityNote": "Note for the activity",
"ActivityDateTime": "yyyy-mm-dd hh:mm:ss",
"FirstName": "John",
"LastName" : "Smith",
"Phone" : "+919845098450",
"Score": 10
}

我是PHP的新手。我尝试了以下方法来创建一个键值对。下面发布的示例中没有一个在服务器上执行POST [我在服务器上找不到我的POST更新,即leadquared cloud]。

我的示例数据。我在这里提到的例子中使用了以下示例数据。

$firstName='Test5';
$activityEvent=201;
$emailAddress='test10@test.com';
$activityNote='Note note note';
$phone='9551653808';
$date='2015-07-21 12:48:10';

示例1:`

  $data_string = '[
        {"ActivityEvent": "'.$activityEvent.'"},
        {"EmailAddress": "'.$emailAddress.'"},
        {"ActivityNote": "'.$activityNote.'"},
        {"Phone": "'.$phone.'"},
        {"ActivityDateTime","'.$date.'"}
      ]';

示例2:

      $data_string['ActivityEvent']=$activityEvent;
      $data_string['EmailAddress']=$emailAddress;
      $data_string['ActivityNote']=$activityNote;
      $data_string['Phone']=$phone;
      $data_string['ActivityDateTime']=$date;

示例3:

          $datastring = array(
          'ActivityEvent' => $activityEvent,
          'EmailAddress' => $emailAddress,
          'ActivityNote' => $activityNote,
          'Phone' => $phone,
          'ActivityDateTime' => $date);

一旦示例正确,我将数据字符串发布到以下代码。

try
{
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
                      'Content-Type:application/json',
                      'Content-Length:'.strlen($data_string)
                      ));
$json_response = curl_exec($curl);
curl_close($curl);
} catch (Exception $ex) {
 curl_close($curl);
}

2 个答案:

答案 0 :(得分:2)

使用

$datastring = array(
  'ActivityEvent' => $activityEvent,
  'EmailAddress' => $emailAddress,
  'ActivityNote' => $activityNote,
  'Phone' => $phone,
  'ActivityDateTime' => $date
);
json_encode($datastring);

参考this链接。

你只需要改变

  curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);

  curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data_string));

答案 1 :(得分:2)

因为您正在使用'Content-Type:application / json',所以您需要提供json数组而不是php数组 首先通过

获取php格式的数组
$datastring=
array(
"EmailAddress"=> "john.smith@acmeconsulting.co",
"ActivityEvent"=> 112,
"ActivityNote"=> "Note for the activity",
"ActivityDateTime"=> "yyyy-mm-dd hh:mm:ss",
"FirstName"=> "John",
"LastName" => "Smith",
"Phone" => "+919845098450",
"Score"=> 10
);

然后使用php json编码函数

$value=json_encode($datastring)
echo $value;
$value will be your json encoded array
{
"EmailAddress":"john.smith@acmeconsulting.co",
"ActivityEvent":112,
"ActivityNote":"Note for the activity","ActivityDateTime":"yyyy-mm-dd hh:mm:ss",
"FirstName":"John",
"LastName":"Smith",
"Phone":"+919845098450",
"Score":10
}

将$ datastring替换为代码中的$ value为

curl_setopt($curl, CURLOPT_HTTPHEADER, array(
                      'Content-Type:application/json',
                      'Content-Length:'.strlen($value)
                      ));