Rumah > pembangunan bahagian belakang > tutorial php > 使用 PHP的curl扩展提交 json 格式数据

使用 PHP的curl扩展提交 json 格式数据

WBOY
Lepaskan: 2016-07-29 09:13:31
asal
806 orang telah melayarinya

实例代码

<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>
Salin selepas log masuk

返回结果:

<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>
Salin selepas log masuk

说明:

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教程有兴趣的朋友有所帮助。

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan