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.