PHP uses the CURL method to POST data to the API code

Source: Internet
Author: User
PHP uses the curl method to post data to the api interface. most of the HTTP requests of the API are GET. therefore, no matter AJAX or PHP is used for secondary processing, the returned data can be obtained, however, the HTTP request method of some APIs is POST, so we need to use curl, which is also relatively simple. the code is as follows:

 'Thekeyvalue'); $ json_data = postData ($ url, $ data); $ array = json_decode ($ json_data, true); echo'
'; Print_r ($ array); function postData ($ url, $ data) {$ ch = curl_init (); $ timeout = 300; curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, CURLOPT_REFERER, "http://www.jb51.net/"); // Construct the Origin curl_setopt ($ ch, CURLOPT_POST, true); curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, $ timeout); $ handles = curl_exec ($ ch); curl _ Close ($ ch); return $ handles ;}?>

For more articles about the code that uses the CURL method to POST data to the API in PHP, refer to the PHP Chinese website!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.