Php sends post request function sharing. Copy the code as follows: functiondo_post_request ($ url, $ data, $ optional_headersnull) {$ paramsarray (httparray (methodPOST, content $ data); if ($ optional _
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;
}
The usage is as follows:
The code is as follows:
// Json string
$ Data = "{...}";
// Convert to an array
$ Data = json_decode ($ data, true );
$ Postdata = http_build_query ($ data );
Do_post_request ("http: // localhost", $ postdata );
The http://www.bkjia.com/PHPjc/736791.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/736791.htmlTechArticle code is as follows: function do_post_request ($ url, $ data, $ optional_headers = null) {$ params = array ('http' = array ('method' = 'post ', 'content' = $ data); if ($ optional _...