You need to request an API in the recent work, because you have previously taken a similar function directly to write the functions used. But the data is not passed, one return to err.
The code is as follows:
1 functionPost_params ($url,$params,$headers) { 2 $ch=Curl_init (); 3curl_setopt ($ch, Curlopt_post, 1); 4curl_setopt ($ch, Curlopt_url,$url); 5curl_setopt ($ch, Curlopt_postfields,$params); 6curl_setopt ($ch, Curlopt_returntransfer, 1);7curl_setopt ($ch, Curlopt_httpheader,$headers);8 $json= Curl_exec ($ch);9Curl_close ($ch);Ten return $json; One}
View Code
It doesn't seem to be a problem, but it just doesn't work.
After seeing a similar solution on the Internet, I changed the code with a try mentality:
1 functionCurlpost ($url,$headers,$post){2 $ch=curl_init ();3curl_setopt ($ch, Curlopt_url,$url);4 if(!Empty($headers)){5curl_setopt ($ch, Curlopt_httpheader,$headers);6 }7curl_setopt ($ch, Curlopt_post, 1);//POST8curl_setopt ($ch, Curlopt_postfields,9 Http_build_query($post));//PostDataTencurl_setopt ($ch, curlopt_returntransfer,1);//Success Onecurl_setopt ($ch, curlopt_header,0); Acurl_setopt ($ch, curlinfo_header_out,1); - $json= Curl_exec ($ch); - return $json; the}
View Code
then re-request, OK ...
What's the situation?
My understanding is that when Curlopt_postfields is set to an array, Content-type is set to Multipart/form-data;
When using string values, Content-type is application/x-www-data-urlencoded.
The function of the http_build_query () function added in the code is to generate a Url-encode request string using the given association (or subscript) and array;
Notation format: http_build_query (mixed $query _data [, String $numeric _prefix [, String $arg _separator [, int $enc _type = Php_query_ RFC1738]])
For example: $data = Array ("name" = "Callback", "value" = "test");
$rescult = Http_build_query ($data);
We output the $rescutl to get:
Name=callback&value=test
What's the use of this, this is the analog HTTP request, the resulting data through the function Url-encode, is generally used in callbacks. Finish
Conclusion: The API receives a string! Http_build_query () transfers the array to a string.
RELATED Links: 48983929
Finish
Code codes or to calm down to think about the mind without distractions is the key to the logic ...
PHP Post request string and array value difference