phenomena
In the recent work encountered a problem, is to use post to send a request, the post data dead and dead transmission, has been the request to return error.
The code is as follows:
$post = Array (' userid ' => 1000034443,);
function Curlpost ($url, $headers, $post) {$ch = Curl_init ();
curl_setopt ($ch, Curlopt_url, $url);
if (!empty ($headers)) {curl_setopt ($ch, Curlopt_httpheader, $headers); curl_setopt ($ch, Curlopt_post, 1);//set to POST mode curl_setopt ($ch, Curlopt_postfields, $post);//post data Curl_seto PT ($ch, curlopt_returntransfer,1);
If success returns only the results, nothing is automatically exported.
curl_setopt ($ch, curlopt_header,0);//If you want to include a header in the output, set this option to a value other than 0.
curl_setopt ($ch, curlinfo_header_out,1);//request string to track handle when enabled.
$json = curl_exec ($ch);
$headers = Curl_getinfo ($ch, curlinfo_header_out);
echo "\n\n===== request return =====\n"; echo "Out Headers:\t". $headers. "
\ n ";
$hearLen = Curl_getinfo ($ch, curlinfo_header_size); echo "Header Len:\t". $hearLen. "
\ n ";
$statuscode = Curl_getinfo ($ch, Curlinfo_http_code); echo "Httpcode:\t". $statuscode. "
\ n ";
echo "\n===================\n";
return $json;
@curl_close ($ch); }
What's wrong with that? The
results are as follows:
it feels fine. How could it be passed? And then you modify the code by feeling
function curlpost ($url, $headers, $post) {$ch = Curl_init ();
curl_setopt ($ch, Curlopt_url, $url);
if (!empty ($headers)) {curl_setopt ($ch, Curlopt_httpheader, $headers); curl_setopt ($ch, Curlopt_post, 1);//set to POST mode curl_setopt ($ch, Curlopt_postfields, Http_build_query ($post));//po St Data curl_setopt ($ch, curlopt_returntransfer,1);
If success returns only the results, nothing is automatically exported.
curl_setopt ($ch, curlopt_header,0);//If you want to include a header in the output, set this option to a value other than 0.
curl_setopt ($ch, curlinfo_header_out,1);//request string to track handle when enabled.
$json = curl_exec ($ch);
$headers = Curl_getinfo ($ch, curlinfo_header_out);
echo "\n\n===== request return =====\n"; echo "Out Headers:\t". $headers. "
\ n ";
$hearLen = Curl_getinfo ($ch, curlinfo_header_size); echo "Header Len:\t". $hearLen. "
\ n ";
$statuscode = Curl_getinfo ($ch, Curlinfo_http_code); echo "Httpcode:\t". $statuscode. "
\ n ";
echo "\n===================\n";
return $json;
@curl_close ($ch); }
It's not the same.
Look at the effect first:
The comparison found that the content type value is different ...
What's going on here.
When Curlopt_postfields is set to an array, the HTTP header sends the content_type:application/x-www-form-urlencoded, which is the normal page when submitting the form when the browser sends the head, And multipart/form-data We know that this is a form for uploading files, including the boundary delimiter, which can be a lot more bytes.
The manual mentions:
The full data to post in a HTTP "post" operation. To post a file, prepend a filename with @ and use the full path. This can either is passed as a urlencoded string like ' Para1=val1¶2=val2& ... ' or as an array with the field name As key and field data as value. If value is a array, the Content-type header would be set to Multipart/form-data.
When you use an array to provide post data, the default is to set the Content_Type to Multipart/form-data, although it does not affect most Web servers, but there are a few servers that are not compatible. Conclusions
This time I did not return to Multipart/form-data, but this is really the problem, suggest that post data can use the Http_build_query () function