196

ここに私のコードがあります、

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

そして別のページでは、投稿データを取得しています。

    print_r ($_POST);

出力は

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 ( ) 

そのため、自分のサーバーでも適切なデータを取得していません。空の配列です。http://docs.shopify.com/api/customer#createのように json を使用して REST を実装したい

4

7 に答える 7

267

あなたはjsonを間違ってPOSTしています-しかし、たとえそれが正しかったとしても、使用してテストすることはできませんprint_r($_POST)(理由はこちらを読んでください)。file_get_contents("php://input")代わりに、2 番目のページで、POSTed json を含むを使用して受信リクエストを取得できます。受信したデータをより読みやすい形式で表示するには、次のことを試してください。

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

コードでは、 を示しContent-Type:application/jsonていますが、すべての POST データを json エンコードしているわけではありません。「顧客」POST フィールドの値のみです。代わりに、次のようにします。

$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>";

補足: Shopify API を自分で直接操作する代わりに、サードパーティのライブラリを使用することでメリットが得られる場合があります。

于 2013-03-26T17:26:44.130 に答える
20

交換

curl_setopt($ch, CURLOPT_POSTFIELDS, array("customer"=>$data_string));

と:

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

「他のページ」の意味がわかりません。「url_to_post」のページであることを願っています。そのページが PHP で記述されている場合、上記で投稿した JSON は次のように読み取られます。

$jsonStr = file_get_contents("php://input"); //read the HTTP body.
$json = json_decode($jsonStr);
于 2012-06-18T08:34:09.840 に答える
2

この例を試してください。

<?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;
?>

あなたの page2.php コード

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

?>
于 2012-06-18T09:05:03.907 に答える
2

このようにしてみてください:

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

忘れていた重要なことは、データを json_encode することでした。ただし、curl_setopt_array を使用して、配列を渡すことですべての curl オプションを一度に設定すると便利な場合もあります。

于 2012-06-18T08:38:37.670 に答える