PHP Curl's Curlopt_postfields array and string puzzle ____php

Source: Internet
Author: User
Tags curl
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&para2=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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.