首頁 > 後端開發 > php教程 > 使用 PHP的curl擴充功能提交 json 格式數據

使用 PHP的curl擴充功能提交 json 格式數據

WBOY
發布: 2016-07-29 09:13:31
原創
806 人瀏覽過

實例代碼

<code><span>$ch</span> = curl_init();
curl_setopt_array(<span>$ch</span>, [
        CURLOPT_RETURNTRANSFER=><span>true</span>,
        CURLOPT_POST=><span>true</span>,
        CURLOPT_HEADER=><span>false</span>,
        CURLOPT_TIMEOUT=><span>30</span>,
        CURLOPT_HTTPHEADER=>[<span>"Content-type:application/json"</span>],
        CURLOPT_URL=><span>"http://localhost/city"</span>,
        CURLOPT_POSTFIELDS=><span>'{"province_id":39}'</span>,
        ]);

<span>$result</span> = curl_exec(<span>$ch</span>);

print_r(<span>$result</span>);</code>
登入後複製

回傳結果:

<code>{"<span>code</span>":<span><span>1</span></span>,"<span>data</span>":<span>[{"<span>id</span>":<span><span>"40"</span></span>,"<span>name</span>":<span><span>"石家庄市"</span></span>},{"<span>id</span>":<span><span>"64"</span></span>,"<span>name</span>":<span><span>"唐山市"</span></span>},{"<span>id</span>":<span><span>"80"</span></span>,"<span>name</span>":<span><span>"秦皇岛市"</span></span>},{"<span>id</span>":<span><span>"89"</span></span>,"<span>name</span>":<span><span>"邯郸市"</span></span>},{"<span>id</span>":<span><span>"110"</span></span>,"<span>name</span>":<span><span>"邢台市"</span></span>},{"<span>id</span>":<span><span>"131"</span></span>,"<span>name</span>":<span><span>"保定市"</span></span>},{"<span>id</span>":<span><span>"158"</span></span>,"<span>name</span>":<span><span>"张家口市"</span></span>},{"<span>id</span>":<span><span>"177"</span></span>,"<span>name</span>":<span><span>"承德市"</span></span>},{"<span>id</span>":<span><span>"190"</span></span>,"<span>name</span>":<span><span>"沧州市"</span></span>},{"<span>id</span>":<span><span>"208"</span></span>,"<span>name</span>":<span><span>"廊坊市"</span></span>},{"<span>id</span>":<span><span>"220"</span></span>,"<span>name</span>":<span><span>"衡水市"</span></span>}]</span>}</code>
登入後複製

說明:

CURLOPT_HEADER 的預設值就是 false,所以上面這個選項可以不設定
如果不傳遞json格式的數據,可以使用http_build_query建立CURLOPT_POSTFIELDS的值(形式類似name=tom&age=25)

').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介紹了使用 PHP的curl擴充功能提交 json 格式數據,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板