相关文章推荐
另类的木瓜  ·  使用 Oracle RAC 和 PHP ...·  1 年前    · 
挂过科的香菜  ·  pg数据库if语句-掘金·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams $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 at other page, I am retrieving post data.

    print_r ($_POST);

Output is

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 am not getting proper data even at my own server, it's empty array. I want to implement REST using json as at http://docs.shopify.com/api/customer#create

Aren't You missing converting $data to $data_string using json_encode()??? Do not see this line of code... – shadyyx Jun 18, 2012 at 8:20 Sorry i didn't write here but in my code i wrote code $data_string=json_encode($data); code and how to write code in comments?in comments i can't write line break and so how to write code? – user1463076 Jun 18, 2012 at 8:37

You are POSTing the json incorrectly -- but even if it were correct, you would not be able to test using print_r($_POST) (read why here). Instead, on your second page, you can nab the incoming request using file_get_contents("php://input"), which will contain the POSTed json. To view the received data in a more readable format, try this:

echo '<pre>'.print_r(json_decode(file_get_contents("php://input")),1).'</pre>';

In your code, you are indicating Content-Type:application/json, but you are not json-encoding all of the POST data -- only the value of the "customer" POST field. Instead, do something like this:

$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 might benefit from using a third-party library instead of interfacing with the Shopify API directly yourself.

so you dont have to explicitly specify it is POST request? Its known because CURLOPT_POSTFIELDS is set? – Srneczek Nov 26, 2015 at 18:16 where was this answer when I was looking for it all week last week? Now I find it way after I had to figure out myself! – pythonian29033 Sep 1, 2016 at 11:38 Sidenote: If you send JSON and expect JSON as response, then some APIs require setting the response type as well curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json', 'Accept:application/json')); (otherwise you may send JSON, but get XML as answer). – pixelbrackets Mar 9, 2018 at 12:27
$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" ) );
$postdata = json_encode($data);
$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch);
curl_close($ch);
print_r ($result);

This code worked for me. You can try...

The correct JSON encoding would be have an array like this $data = array("customer"=> array("first_name" => "First name", "last_name" => "last name", ... – H Aßdøµ May 26, 2022 at 11:19
$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 dont get what you meant by "other page", I hope it is the page at: 'url_to_post'. If that page is written in PHP, the JSON you just posted above will be read in the below way:

$jsonStr = file_get_contents("php://input"); //read the HTTP body.
$json = json_decode($jsonStr);
                Why would you assume that? If he's putting the data in the 'customer' field, he must be doing so for a reason, no?
– Okonomiyaki3000
                Jun 18, 2012 at 8:41
                Yes, thanks, I missed that part. But he, IMO, is doing it wrong. I will update my answer with it.
– UltraInstinct
                Jun 18, 2012 at 8:43
$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(array("customer" =>$data));
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
echo "$result";
  • always define certificates with CURLOPT_CAPATH option,

  • decide how your POSTed data will be transfered.

    1 Certificates

    By default:

  • CURLOPT_SSL_VERIFYHOST == 2 which "checks the existence of a common name and also verify that it matches the hostname provided" and

  • CURLOPT_VERIFYPEER == true which "verifies the peer's certificate".

    So, all you have to do is:

    const CAINFO = SERVER_ROOT . '/registry/cacert.pem';
    \curl_setopt($ch, CURLOPT_CAINFO, self::CAINFO);
    

    taken from a working class where SERVER_ROOT is a constant defined during application bootstraping like in a custom classloader, another class etc.

    Forget things like \curl_setopt($handler, CURLOPT_SSL_VERIFYHOST, 0); or \curl_setopt($handler, CURLOPT_SSL_VERIFYPEER, 0);.

    Find cacert.pem there as seen in this question.

    2 POST modes

    There are actually 2 modes when posting data:

  • the data is transfered with Content-Type header set to multipart/form-data or,

  • the data is a urlencoded string with application/x-www-form-urlencoded encoding.

    In the first case you pass an array while in the second you pass a urlencoded string.

    multipart/form-data ex.:

    $fields = array('a' => 'sth', 'b' => 'else');
    $ch = \curl_init();
    \curl_setopt($ch, CURLOPT_POST, 1);
    \curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
    

    application/x-www-form-urlencoded ex.:

    $fields = array('a' => 'sth', 'b' => 'else');
    $ch = \curl_init();
    \curl_setopt($ch, CURLOPT_POST, 1);
    \curl_setopt($ch, CURLOPT_POSTFIELDS, \http_build_query($fields));
    

    http_build_query:

    Test it at your command line as

    user@group:$ php -a
    php > $fields = array('a' => 'sth', 'b' => 'else');
    php > echo \http_build_query($fields);
    a=sth&b=else
    

    The other end of the POST request will define the appropriate mode of connection.

    $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've forgotten was to json_encode your data. But you also may find it convenient to use curl_setopt_array to set all curl options at once by passing an array.

    -1. Check the API here: api.shopify.com/customer.html#create. The body that server expects in JSON, not urlencoded-json. Check my answer, no need to use array(..) in `CURLOPT_POSTFIELDS – UltraInstinct Jun 18, 2012 at 8:40 Yes, as I said he is sending it wrong. Passing an array(..) to CURLOPT_POSTFIELDS` will urlencode the JSON too. – UltraInstinct Jun 18, 2012 at 8:46 Anyway, i tried many times with different code but i was not able to do in json now i did successfully in xml. – user1463076 Jun 19, 2012 at 5:58 $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 page2.php code

    $datastring = $_POST['customer']; $data = json_decode( urldecode( $datastring));

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.

  •