命令行 curl

  1. curl -X POST -d 'username=php&password=xxxx' http://httpbin.org/post
    
    {
    
     "args": {},
    
     "data": "",
    
     "files": {},
    
     "form": {
    
       "password": "xxxx",
    
       "username": "php"
    
     },
    
     "headers": {
    
       "Connection": "close",
    
       "Content-Length": "26",
    
       "Content-Type": "application/x-www-form-urlencoded",
    
       "Host": "httpbin.org"
    
     },
    
     "json": null,
    
     "origin": "139.196.111.200",
    
     "url": "http://httpbin.org/post"
    
    }


file_get_contents

  1. function send_post($url, $post_data) {  
    
     $postdata = http_build_query($post_data);  
    
     $options = array(  
    
       'http' => array(  
    
         'method' => 'POST',  
    
         'header' => 'Content-type:application/x-www-form-urlencoded',  
    
         'content' => $postdata,  
    
         'timeout' => 15 * 60
    
       )  
    
     );  
    
     $context = stream_context_create($options);  
    
     $result = file_get_contents($url, false, $context);  
    
     return $result;  
    
    }  
    
    $post_data = array(  
    
     'username' => 'php',  
    
     'password' => 'xxxx'  
    
    );  
    
    $res = send_post('http://httpbin.org/post', $post_data);
    
    print_r($res);
    
    {
    
     "args": {},
    
     "data": "",
    
     "files": {},
    
     "form": {
    
       "password": "xxxx",
    
       "username": "php"
    
     },
    
     "headers": {
    
       "Connection": "close",
    
       "Content-Length": "26",
    
       "Content-Type": "application/x-www-form-urlencoded",
    
       "Host": "httpbin.org"
    
     },
    
     "json": null,
    
     "origin": "139.196.111.200",
    
     "url": "http://httpbin.org/post"
    
    }


    php curl

  1.  
function curl_request($URL,$type,$params,$headers=''){

       $ch = curl_init();

       $timeout = 5; //设置过期时间

       curl_setopt($ch, CURLOPT_URL, $URL);

       if($headers){

           curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

       }

       curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //以文件流的形式返回

       curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

       switch ($type){

           case "GET" : curl_setopt($ch, CURLOPT_HTTPGET, true);break;

           case "POST": curl_setopt($ch, CURLOPT_POST,true);

                    curl_setopt($ch, CURLOPT_POSTFIELDS,$params);break;

           case "PUT" : curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "PUT");

                    curl_setopt($ch, CURLOPT_POSTFIELDS,$params);break;

           case "DELETE":curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "DELETE");

                     curl_setopt($ch, CURLOPT_POSTFIELDS,$params);break;

       }

       $file_contents = curl_exec($ch);//获得返回值

       curl_close($ch);

       return $file_contents;

   }

$res = curl_request('http://httpbin.org/post', 'POST', $post_data);


socket

  1. function request_by_socket($remote_server,$remote_path,$post_string,$port = 80,$timeout = 30) {  
    
     $socket = fsockopen($remote_server, $port, $errno, $errstr, $timeout);  
    
     if (!$socket) die("$errstr($errno)");  
    
     fwrite($socket, "POST $remote_path HTTP/1.0");  
    
     fwrite($socket, "User-Agent: Socket Example");  
    
     fwrite($socket, "HOST: $remote_server");  
    
     fwrite($socket, "Content-type: application/x-www-form-urlencoded");  
    
     fwrite($socket, "Content-length: " . (strlen($post_string) + 8) . "");  
    
     fwrite($socket, "Accept:*/*");  
    
     fwrite($socket, "");  
    
     fwrite($socket, $post_string);  
    
     fwrite($socket, "");  
    
     $header = "";  
    
     while ($str = trim(fgets($socket, 4096))) {  
    
       $header .= $str;  
    
     }  
    
     $data = "";  
    
     while (!feof($socket)) {  
    
       $data .= fgets($socket, 4096);  
    
     }  
    
     return $data;  
    
    }