Copy codeThe 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:
Copy codeThe 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 );