简体   繁体   中英

JSON PHP CURL POSTING

I'm trying to post some JSON data in PHP on my local server. I have the following code below but it's not working. Did i miss a vital thing?

<?php

 $postRequest = array ( 
      
    // Every array will be converted 
    // to an object 
       array( 
        "Attribute" => "FirstName", 
        "Value" => "maheshr"
    ),
       array( 
        "Attribute" => "LastName", 
        "Value" => "netha"
    ),array( 
        "Attribute" => "EmailAddress", 
        "Value" => "mahesh3@simandhareducation.com"
    ),array( 
        "Attribute" => "Phone", 
        "Value" => "9553754571"
    ),
    array( 
        "Attribute" => "Source", 
        "Value" => "Website"
    ), 
    array( 
        "Attribute" => "SearchBy", 
        "Value" => "Phone"
    ), 
    array( 
        "Attribute" => "mx_City", 
        "Value" => "Hyderabad"
    ) , 
    array( 
        "Attribute" => "mx_Course", 
        "Value" => "CPA"
    )
);

$postRequest = json_encode($postRequest);
// echo $postRequest;

$cURLConnection = curl_init('url');

curl_setopt($cURLConnection, CURLOPT_POSTFIELDS, $postRequest);
curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);
$apiResponse = curl_exec($cURLConnection);
curl_close($cURLConnection);
// $apiResponse - available data from the API request
$jsonArrayResponse = json_decode($apiResponse);
echo $jsonArrayResponse;

?>

I am not able to get any response from the curl request. Can anybody help me ?

Thank you.

To post JSON data, you setup header before call curl_exec() like this:

curl_setopt($cURLConnection, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
    ));

You can reference here : How to POST JSON Data With PHP cURL?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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