php post requests using analog curl

Ado, directly on the code, make a note of.

$url="http://localhost/header_server.php";
$body = array("mobile"=>"13899999999", "username"=>"Nick");
$header = array("Content-Type:multipart/x-www-form-urlencoded", "token:test", "client:h5");
Result $ = curlPost ( $ URL , $ body ,. 5, $ header , 'JSON' );
 var_dump ( $ Result ); 


/ * * 
 * array incoming HTTP POST request 
 * / 
function curlPost ( $ URL , $ post_data = Array (), $ timeout = 5, $ header = "", $ data_type = "" ) {
     $ header = empty ( $ header ?) '': $ header ;
     // support json data submitted 
    IF ( $ data_type == 'json'){
        $post_string = json_encode($post_data);
    }elseif($data_type == 'array') {
        $post_string = $post_data;
    }elseif(is_array($post_data)){
        $post_string = http_build_query($post_data, '', '&');
    }
    
    $ch = curl_init();    // 启动一个CURL会话
    curl_setopt($ch, CURLOPT_URL, $url);     //To access the address 
    curl_setopt ( $ CH , CURLOPT_SSL_VERIFYPEER, false );   // inspection certificates sources // https request does not verify the certificate and hosts 
    curl_setopt ( $ CH , CURLOPT_SSL_VERIFYHOST, false );   // check the SSL encryption algorithm from the certificate whether there 
    curl_setopt ( $ CH , CURLOPT_USERAGENT, $ _SERVER [ 'the HTTP_USER_AGENT']); // simulated user's browser 
    // curl_setopt ($ curl, CURLOPT_FOLLOWLOCATION, 1); // use automatically jump 
    // curl_setopt ($ curl , CURLOPT_AUTOREFERER, 1); // automatically set the Referer 
    curl_setopt ( $ CH , CURLOPT_POST, to true ); // send a request for a conventional Post
    curl_setopt ( $ CH , CURLOPT_POSTFIELDS, $ post_string );      // packet Post submitted 
    curl_setopt ( $ CH , CURLOPT_CONNECTTIMEOUT, $ timeout );      // Set the timeout limit prevents infinite loop 
    curl_setopt ( $ CH , CURLOPT_TIMEOUT, $ timeout );
     // curl_setopt ($ curl, CURLOPT_HEADER, 0 ); // return the display region of the content of header 
    curl_setopt ( $ CH , CURLOPT_RETURNTRANSFER, to true );      // acquired information returned as stream file 
    curl_setopt ( $ CH , CURLOPT_HTTPHEADER, $ header ); // simulation header header
    Result $ = the curl_exec ( $ CH ); 

    // header information of the print request 
    // $ curl_getinfo A = (CH $); 
    // var_dump ($ A); 

    curl_close ( $ CH );
     return  $ Result ; 
}

 Some experience:

1. Regardless of "Content-Type: multipart / form-data" or "Content-Type: application / x-www-form-urlencoded" as long as the use of post send data, and the data in the body member is an array format, then the receiving end can use $ _POST acquired.
2. At the receiving end using file_get_contents: when ( "php // input") received only the acquired volume data string type body.
3. When the request to add the parameter in the header XXX, can be used at the receiving end $ _SERVER [ "HTTP_XXX"] is acquired.

Guess you like

Origin www.cnblogs.com/iverson-3/p/11866673.html
Recommended