How to get POST JSON data using PHP cURL?
Here is my code
$url = 'url_to_post'; $data = array("first_name" => "First name","last_name" => "last name","email"=>"email@gmail.com","addresses" => array ("address1" => "some address" ,"city" => "city","country" => "CA", "first_name" => "Mother","last_name" => "Lastnameson","phone" => "555-1212", "province" => "ON", "zip" => "123 ABC" ) ); $data_string = json_encode($data); $ch=curl_init($url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, array("customer"=>$data_string)); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json', 'Content-Length: ' . strlen($data_string)) ); $result = curl_exec($ch); curl_close($ch); And on another page I get data for publication.
print_r ($_POST); Exit
HTTP/1.1 200 OK Date: Mon, 18 Jun 2012 07:58:11 GMT Server: Apache X-Powered-By: PHP/5.3.6 Vary: Accept-Encoding Connection: close Content-Type: text/html Array ( ) So, I do not get the correct data even on my own server, it is an empty array. I want to implement REST using json, as at http://docs.shopify.com/api/customer#create
You didn’t load json correctly - but even if it were correct, you couldn’t test using print_r($_POST) ( read why here ). Instead, on the second page, you can dial an incoming request using file_get_contents("php://input") , which will contain POSTed json . To view the data in a more readable format, try the following:
echo '<pre>'.print_r(json_decode(file_get_contents("php://input")),1).'</pre>'; In your code you specify Content-Type:application/json , but you are not json-encoding all the POST data - only the value of the client's POST field. Instead, do the following:
$ch = curl_init( $url ); # Setup request to send json via POST. $payload = json_encode( array( "customer"=> $data ) ); curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload ); curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); # Return response instead of printing. curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); # Send request. $result = curl_exec($ch); curl_close($ch); # Print response. echo "<pre>$result</pre>"; Sidenote: You can use a third-party library instead of interacting directly with the Shopify API.
Replace
curl_setopt($ch, CURLOPT_POSTFIELDS, array("customer"=>$data_string)); from:
$data_string = json_encode(array("customer"=>$data)); //Send blindly the json-encoded string. //The server, IMO, expects the body of the HTTP request to be in JSON curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); I don’t understand what you mean by “another page”, I hope this page is: 'url_to_post'. If this page is written in PHP, the JSON you just posted above will read as follows:
$jsonStr = file_get_contents("php://input"); //read the HTTP body. $json = json_decode($jsonStr); Try this example.
<?php $url = 'http://localhost/test/page2.php'; $data = array("first_name" => "First name","last_name" => "last name","email"=>"email@gmail.com","addresses" => array ("address1" => "some address" ,"city" => "city","country" => "CA", "first_name" => "Mother","last_name" => "Lastnameson","phone" => "555-1212", "province" => "ON", "zip" => "123 ABC" ) ); $ch=curl_init($url); $data_string = urlencode(json_encode($data)); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, array("customer"=>$data_string)); $result = curl_exec($ch); curl_close($ch); echo $result; ?> Your code page2.php
<?php $datastring = $_POST['customer']; $data = json_decode( urldecode( $datastring)); ?> Try it like this:
$url = 'url_to_post'; // this is only part of the data you need to sen $customer_data = array("first_name" => "First name","last_name" => "last name","email"=>"email@gmail.com","addresses" => array ("address1" => "some address" ,"city" => "city","country" => "CA", "first_name" => "Mother","last_name" => "Lastnameson","phone" => "555-1212", "province" => "ON", "zip" => "123 ABC" ) ); // As per your API, the customer data should be structured this way $data = array("customer" => $customer_data); // And then encoded as a json string $data_string = json_encode($data); $ch=curl_init($url); curl_setopt_array($ch, array( CURLOPT_POST => true, CURLOPT_POSTFIELDS => $data_string, CURLOPT_HEADER => true, CURLOPT_HTTPHEADER => array('Content-Type:application/json', 'Content-Length: ' . strlen($data_string))) )); $result = curl_exec($ch); curl_close($ch); The key thing you forgot is json_encode your data. But it may also be convenient for you to use curl_setopt_array to set all curl parameters at once by passing an array.