Post requests for multiple data encoding methods using curl under PHP code

Source: Internet
Author: User
Tags curl

PHP uses Curl request data is very common, but according to the http/1.1 protocol in the post submission data encoding method, the use of Curl function parameters of the choice is also different.
The Content-type in the header headers of the request message is labeled as the transmission encoding for service-side identification, and the following content-type are used to transmit data correctly according to the different Curl

I. application/x-www-form-urlencoded mode:

1. Common similar Web form data:
Curl Method:

Public Function Curl_post ($data, $url) {
    $ch = Curl_init ();

    curl_setopt ($ch, Curlopt_url, $url);
    curl_setopt ($ch, Curlopt_customrequest, "POST");
    curl_setopt ($ch, Curlopt_ssl_verifypeer, FALSE);
    curl_setopt ($ch, Curlopt_ssl_verifyhost, FALSE);
    curl_setopt ($ch, Curlopt_useragent, ' mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0) ');
    curl_setopt ($ch, curlopt_followlocation, 1);
    curl_setopt ($ch, Curlopt_autoreferer, 1);
    curl_setopt ($ch, Curlopt_postfields, $data);
    curl_setopt ($ch, Curlopt_returntransfer, true);
    $tmpInfo = curl_exec ($ch);
    if (Curl_errno ($ch)) {
    echo ' errno '. Curl_error ($ch);
    }
    Curl_close ($ch);
    return $tmpInfo;
}

Processing Example:

Request:

Public Function Test2 () {            
        $template = array (
            ' touser ' => ' test1 ', '
            template_id ' => ' one ', '
            url ' = > ' http://www.test.com ',
            ' topcolor ' => ' #EEEEEE ',
            ' data ' => array (' T1 ', ' T2 ', ' T3 ')
        ;
        $data = $template;   
        $url = "Http://127.0.0.1/jytest/test1";
        $this->curl_post ($data, $url);    
    

Receive processing:

Public Function test1 () {      
      $res = $_post;
      $content = ';
      $content = $content. $res [' Touser ']. ', ';
      $content = $content. $res [' template_id ']. ', ';
      $content = $content. $res [' url ']. ', ';
      $content = $content. $res [' Topcolor ']. ', ';
      $content = $content. Serialize ($res [' Data ']). ', ';
      $filePath = "./test.txt";      
      File_put_contents ($filePath, $content);

    }

Test.txt file contents are: Test1, one, http://www.test.com, #EEEEEE, s:5: "Array";

You can see that the submitted array is not properly received
If you want to pass an array, you will use the Curl_post method in the

curl_setopt ($ch, Curlopt_postfields, $data);

To

curl_setopt ($ch, Curlopt_postfields, Http_build_query ($data));

2. JSON, using the above Curl_post method unchanged, just the JSON content as a string content delivery, to have a header field name corresponding to it, to receive

Processing Example:

Public Function Test2 () {
    //requestor
    $template = Array (
        ' touser ' => ' test1 ',
        ' template_id ' => ' 11 ',
        ' url ' => ' http://www.test.com ', '
        topcolor ' => ' #EEEEEE ',
        ' data ' => array (' T1 ', ' T2 ',
    ' T3 ') ;
    $data [' data '] = Json_encode ($template);
    $url = "Http://127.0.0.1/test/test1";
    $res =  $this->curl_post ($data, $url);
    Print_r (Json_decode ($res, 1));

 }

Receive processing:

Public Function test1 () {
     //Receive processing return
      $data = Json_decode ($_post[' data '],true);    
      $content =serialize ($data);
      $filePath = "./test.txt";      
      File_put_contents ($filePath, $content);    
    }

The contents of the Test.txt file are:
A:5:{s:6: "Touser"; s:3: "Test1"; s:11: "template_id"; S:2: "One"; s:3: "url"; s:19: "http://www.test.com"; s:8: "Topcolor"; s:7: "#EEEEEE"; s:4: "Data"; A:3:{i:0;s:2: "T1"; I:1;s:2: "T2"; I:2;s:2: "T3";}}
Can be seen as the contents of the array $template

two. Direct Application/json mode:

Curl Set Header to Content-type to Application/json

Curl method

Private Function Curl_postjson ($data, $urlcon) {
        $ch = Curl_init ($urlcon);//Request URL address
        curl_setopt ($ch, curlopt _customrequest, "POST");
        curl_setopt ($ch, Curlopt_postfields, $data);//$data JSON type string
        curl_setopt ($ch, Curlopt_returntransfer, true);
        curl_setopt ($ch, Curlopt_httpheader, Array (' Content-type:application/json ', ' content-length: '. strlen ($data)));
        $tmpInfo = curl_exec ($ch);
        Curl_close ($ch);
        return $tmpInfo;
    }

Request:

Public Function Test2 () {      
      //requestor
      $template = Array (
            ' touser ' => ' test1 ',
            ' template_id ' => ' 11 ',
            ' url ' => ' http://www.test.com ', '
            topcolor ' => ' #EEEEEE ',
            ' data ' => array (' T1 ', ' T2 ',
        ' T3 ') ;

      You do not need a separate field name for the JSON array, and the server receives the
      $data = Json_encode ($template) with file_get_contents (' php://input ');  
      $url = "Http://127.0.0.1/test/test1";    
      $res = $this->curl_postjson (Json_encode ($data), $url);
      Print_r (Json_decode ($res, 1));

    }

Receive processing:

Public Function test1 () {     
      $filePath = "./test.txt";   
      You do not need to use field names to receive JSON arrays
      $res =  file_get_contents (' php://input '); 
      $res = Json_decode ($res, 1);
      $content = serialize ($res);
      File_put_contents ($filePath, $content);
      echo Json_encode ($res);

    }

The

Test.txt file contents are:
s:110: "{touser": "Test1", "template_id": "One", "url": "Http:\/\/www.test.com", "Topcolor": "# Eeeeee "," Data ": [" T1 "," T2 "," T3 "]}";

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.