Demo Post,get request and accept request in PHP detailed explanation

Source: Internet
Author: User

In PHP we often use Curl extension to carry out the analog post, get request, the following is to say how to simulate:

First, simulate a POST request:
functionHttp_post_data ($url,$query _data,$timeout=30) {
if (Is_array ($query _data)) {
    $post _str=Http_build_query($query _data);//into a=1&b=2 form will perform urlencode () conversion
}$curl= Curl_init ();//Initialize Curlcurl_setopt ($curl, Curlopt_url,$url); curl_setopt ($curl, Curlopt_header, 0);//Filtering HTTP Headerscurl_setopt ($curl, Curlopt_returntransfer, 1);//Show Output Resultscurl_setopt ($curl, Curlopt_post,true);//Post Modecurl_setopt ($curl, Curlopt_postfields,$post _str);//Post Transfer datacurl_setopt ($curl, Curlopt_connecttimeout, 5);//set the wait timecurl_setopt ($ch, Curlopt_timeout,$timeout);//Set Timeout if(Strtolower(substr($this->url,0,5) = = ' https ') {curl_setopt ($ch, Curlopt_ssl_verifypeer, 0); curl_setopt ($ch, Curlopt_ssl_verifyhost, 0); } //curl_setopt ($curl, Curlopt_cainfo,dirname (__file__). ' /cacert.pem ')//If the address is an HTTPS protocol and Curlopt_ssl_verifypeer and Curlopt_ssl_verifyhost are turned on, the certificate needs to be loaded
Based on the certificate downloaded by HTTP://CURL.HAXX.SE/CA/CACERT.PEM, the option to add the above registration will work fine. $header=Array( "Content-type:application/x-www-form-urlencoded; Charset=utf-8 " ); curl_setopt ($curl, Curlopt_httpheader,$header); $content= Curl_exec ($curl);//return content $status= Curl_getinfo ($curl, Curlinfo_http_code);//Return Status CodeCurl_close ($curl); if($status= = ' 200 ')return $content; Else return false;}

A mock get request is straightforward, changing the post to false, transmitting the post data item annotation, and stitching the requested parameters behind the path.

In this way, if we send an array or a=1&b=2 a string of this format, accept such a transmission, directly with the $_post or $_request can get the corresponding parameter value, as for the other how to say below.

Ii. Different Request headers

But sometimes when we want to transfer JSON and files, the above approach may be a bit unworkable and need to change the request header as follows

Request JSON data:

$header [] = "Content-type:application/json; Charset=utf-8 "; // the content of the claim request is JSON $header [] = "Content-length:". strlen (Json_encode ($data)); // the length of the requested content

Upload file:

$header [] = "Content-type:multipart/form-data";//Use $_files to accept transmitted files

Request HTML Data:

$header [] = "Content-type:text/html;charset=utf-8";

Request XML data:

$header [] = "Content-type:text/xml;charset=utf-8";

Another: This way, if the browser accepts the content, it will be treated as plain text, not HTML or XML parsing.

$header [] = "Content-type:text/plain;charset=utf-8";
Iii. several ways of accepting requests

1. Strings for arrays or a=1&b=2 classes

This does not need to say directly with our commonly used $_get, $_post, $_request can accept

2. JSON, XML, HTML, or other strings

PHP By default only recognizes the application/x-www.form-urlencoded standard data type, the content of the XML content, JSON content, HTML content cannot be parsed into an $_post array, so the prototype will be retained, can be handed to file_get_ Contents (' Php://input ') can also be used $globals[' http_raw_post_data ').

$xml file_get_contents ("Php://input"); // or $xml $GLOBALS [' Http_raw_post_data '];

Php://input allows the raw data to be read from the POST. Compared to $globals[' http_raw_post_data ', it brings less pressure to memory and does not require any special php.ini settings. Neither can be used to receive data in the form of enctype= "Multipart/form-data".

Iv. file_get_contents () Impersonation request
functionPost$url,$content){        $ctx=Array(            ' http ' =Array(                ' Method ' = ' POST ',//or get' Header ' = ' content-type:application/x-www-form-urlencoded ', ' Content ' =$content            )        ); return file_get_contents($url,false,stream_context_create($ctx));}

This method can also quickly simulate the post, get requests, but this way in the case of a small amount of traffic is not a problem, if the volume of traffic may be a problem, so generally in the project is not very recommended this method of request.

Demo Post,get request and accept request in PHP detailed explanation

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.