第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

如何使用PHP curl發(fā)布JSON數(shù)據(jù)?

如何使用PHP curl發(fā)布JSON數(shù)據(jù)?

PHP
ibeautiful 2019-07-17 16:08:02
如何使用PHP curl發(fā)布JSON數(shù)據(jù)?這是我的密碼    $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);在另一頁,我正在檢索帖子數(shù)據(jù)。    print_r ($_POST);輸出HTTP/1.1 200 OKDate: Mon, 18 Jun 2012 07:58:11 GMTServer: ApacheX-Powered-By: PHP/5.3.6Vary: Accept-EncodingConnection:  closeContent-Type: text/htmlArray ( )所以,即使在我自己的服務(wù)器上,我也沒有得到正確的數(shù)據(jù),它是空數(shù)組。我希望使用json實(shí)現(xiàn)REST,如http://docs.shopify.com/api/customer#create
查看完整描述

3 回答

?
互換的青春

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超6個(gè)贊

您正在錯誤地發(fā)布json,但是即使它是正確的,您也無法使用print_r($_POST) (在這里讀為什么)。相反,在第二個(gè)頁面上,您可以使用file_get_contents("php://input"),其中將包含已發(fā)布的json..若要以更易讀的格式查看所接收的數(shù)據(jù),請嘗試如下:

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

在您的代碼中,您指示Content-Type:application/json,但是您不是json-編碼所有的POST數(shù)據(jù)-只有“Customer”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>";

Sideenote:使用第三方圖書館而不是直接與Shopify API接口。


查看完整回答
反對 回復(fù) 2019-07-17
?
料青山看我應(yīng)如是

TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超8個(gè)贊

$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_SSL_VERIFYHOST, 0);curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);curl_setopt($ch, CURLOPT_POST, 1);curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));$result = curl_exec($ch);curl_close($ch);print_r ($result);

這個(gè)密碼對我有用。你可以試試.。


查看完整回答
反對 回復(fù) 2019-07-17
  • 3 回答
  • 0 關(guān)注
  • 438 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號