Oto mój kod,Jak POST dane JSON z PHP cURL?
$url = 'url_to_post';
$data = array("first_name" => "First name","last_name" => "last name","email"=>"[email protected]","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);
A na drugiej stronie, ja pobierania danych post.
print_r ($_POST);
Wyjście jest
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 ()
Więc ja nie dostaję odpowiednie dane nawet na własnym serwerze, to pusta tablica. Chcę, aby zaimplementować REST za pomocą JSON na http://docs.shopify.com/api/customer#create
prawda brakuje konwersji '$ data' do' '$ data_string' używając json_encode()' ??? Nie widzę tej linii kodu ... – shadyyx
Niestety nie napisałem tutaj, ale w moim kodzie napisałem 'code' $ data_string = json_encode ($ data); 'code' i jak pisać kod w komentarzach? w komentarzach nie mogę napisać line break i jak napisać kod? – user1463076