Arun Prakash
Arun Prakash

Reputation: 441

What is the correct way to send key value pair in JSON in PHP

I'm trying to do POST in PHP. In the document, they mentioned the format as below.

{
"EmailAddress": "[email protected]",
"ActivityEvent": 112,
"ActivityNote": "Note for the activity",
"ActivityDateTime": "yyyy-mm-dd hh:mm:ss",
"FirstName": "John",
"LastName" : "Smith",
"Phone" : "+919845098450",
"Score": 10
}

I'm new to PHP. I tried the following ways to create a key value pair.None of the example posted below do a POST on server[ I couldn't find my POST update on the server i.e leadsquared cloud].

My sample data. I used the below sample data in the examples mentioned here.

$firstName='Test5';
$activityEvent=201;
$emailAddress='[email protected]';
$activityNote='Note note note';
$phone='9551653808';
$date='2015-07-21 12:48:10';

Example 1 : `

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

Example 2 :

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

Example 3 :

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

Once the example is right, I'm posting the datastring to the following code.

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

Upvotes: 0

Views: 1329

Answers (2)

Sagar Chapagain
Sagar Chapagain

Reputation: 375

since you are using 'Content-Type:application/json' you need to provide json array not a php array so first get a php formatted array by

$datastring=
array(
"EmailAddress"=> "[email protected]",
"ActivityEvent"=> 112,
"ActivityNote"=> "Note for the activity",
"ActivityDateTime"=> "yyyy-mm-dd hh:mm:ss",
"FirstName"=> "John",
"LastName" => "Smith",
"Phone" => "+919845098450",
"Score"=> 10
);

then use php json encoding function as

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

replace $datastring to $value in your code as

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

Upvotes: 2

Nikunj Chotaliya
Nikunj Chotaliya

Reputation: 802

use

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

refer this link.

you only need to change

  curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);

to

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

Upvotes: 2

Related Questions