PHP uses POST to send data requests to https-PHP source code

Source: Internet
Author: User
To use php to send post requests, we can use curl. This is the simplest and best-performing request method. The following shows an example of using PHP to send data requests to https using POST. To use php to send post requests, we can use curl. This is the simplest and best-performing request method. The following shows an example of using PHP to send data requests to https using POST.

Script ec (2); script

Example

The Code is as follows:

$ Data = '{"type": "news", "offset": 50, "count": 20 }';
$ Access_token = "0 erCbg (112 words omitted here) dzrr0pjbflhahamqw ";
$ Url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material? Access_token = ". $ access_token;

// Send data
$ Ch = curl_init ();
$ Timeout = 300;
Curl_setopt ($ ch, CURLOPT_URL, $ url );
// Curl_setopt ($ ch, CURLOPT_REFERER, "http://www.111cn.net/"); // disguise
Curl_setopt ($ ch, CURLOPT_POST, true );
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, $ timeout );
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, FALSE); // when the URL is set to SSL, this line can solve the page blank.
$ Rs = curl_exec ($ ch );
Curl_close ($ ch );

// Output
$ Rs = json_decode ($ rs );
Echo'

';
print_r($rs);
echo '
';


Example

Method 1 is also clumsy, and it seems difficult to meet my needs. Finally, I forgot which English website I found the following method:

The Code is as follows:
Function do_post_request ($ url, $ data, $ optional_headers = null)
{
$ Params = array ('http' => array (
'Method' => 'post ',
'Content' => $ data
));
If ($ optional_headers! = Null ){
$ Params ['http'] ['head'] = $ optional_headers;
}
$ Ctx = stream_context_create ($ params );
$ Fp = @ fopen ($ url, 'rb', false, $ ctx );
If (! $ Fp ){
Throw new Exception ("Problem with $ url, $ php_errormsg ");
}
$ Response = @ stream_get_contents ($ fp );
If ($ response = false ){
Throw new Exception ("Problem reading data from $ url, $ php_errormsg ");
}
Return $ response;
}


After trying it out, I felt that the effect was very good, concise, and general, and the returned content was only the content in the Body.

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.