[英]How to POST JSON Data With PHP cURL?
這是我的代碼,
$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);
Output 是
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 ( )
所以,即使在我自己的服務器上,我也沒有得到正確的數據,它是空數組。 我想使用 json 實現 REST ,如http://docs.Z329A01FDDB5A552#9C85.com/apicustomer#9C855
您發布的 json 不正確——但即使它是正確的,您也無法使用print_r($_POST)
進行測試(在此處閱讀原因)。 相反,在您的第二頁上,您可以使用file_get_contents("php://input")
獲取傳入請求,其中將包含 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 接口。
$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);
這段代碼對我有用。 你可以試試...
代替
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);
請試試這個代碼:-
$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";
第一的,
始終使用 CURLOPT_CAPATH 選項定義證書,
決定您發布的數據將如何傳輸。
默認情況下:
CURLOPT_SSL_VERIFYHOST == 2
“檢查通用名稱的存在並驗證它是否與提供的主機名匹配”和
CURLOPT_VERIFYPEER == true
“驗證對等方的證書”。
所以,你所要做的就是:
const CAINFO = SERVER_ROOT . '/registry/cacert.pem';
...
\curl_setopt($ch, CURLOPT_CAINFO, self::CAINFO);
取自工作類,其中SERVER_ROOT
是在應用程序引導期間定義的常量,如自定義類加載器、另一個類等。
忘記諸如\\curl_setopt($handler, CURLOPT_SSL_VERIFYHOST, 0);
或\\curl_setopt($handler, CURLOPT_SSL_VERIFYPEER, 0);
.
發布數據時實際上有兩種模式:
數據傳輸時Content-Type
標頭設置為multipart/form-data
或,
數據是帶有application/x-www-form-urlencoded
編碼的 urlencoded 字符串。
在第一種情況下,您傳遞一個數組,而在第二種情況下,您傳遞一個urlencoded string 。
multipart/form-data
例如:$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
例如:$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
:在你的命令行測試它
user@group:$ php -a
php > $fields = array('a' => 'sth', 'b' => 'else');
php > echo \http_build_query($fields);
a=sth&b=else
POST 請求的另一端將定義適當的連接模式。
像這樣嘗試:
$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 選項很方便。
試試這個例子。
<?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));
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.