Home >Backend Development >PHP Tutorial >Submit json format data using PHP's curl extension

Submit json format data using PHP's curl extension

WBOY
WBOYOriginal
2016-07-29 09:13:31817browse

Example code

<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>

Return result:

<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>

Explanation:

The default value of CURLOPT_HEADER is false, so the above option does not need to be set
If you do not pass data in json format, you can use http_build_query to create the value of CURLOPT_POSTFIELDS (in the form similar to name=tom&age=25)

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

The above introduces the use of PHP's curl extension to submit json format data, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:47 PHP magic methodsNext article:47 PHP magic methods