zoukankan      html  css  js  c++  java
  • PHP利用Curl、socket、file_get_contents POST数据 简单

    1 /**  
     2 * 其它版本  
     3 * 使用方法:  
     4 * $post_string = "app=request&version=beta";  
     5 * request_by_other('http://facebook.cn/restServer.php',$post_string);  
     6 */   
     7 function request_by_other($remote_server,$post_string){   
     8     $context = array(   
     9         'http'=>array(   
    10             'method'=>'POST',   
    11             'header'=>'Content-type: application/x-www-form-urlencoded'."\r\n".   
    12                       'User-Agent : Jimmy\'s POST Example beta'."\r\n".   
    13                       'Content-length: '.strlen($post_string)+8,   
    14             'content'=>.$post_string)   
    15          );   
    16     $stream_context = stream_context_create($context);   
    17     $data = file_get_contents($remote_server,FALSE,$stream_context);   
    18      return $data;   
    19 }  
    20    
    21 /**  
    22 * Socket版本  
    23 * 使用方法:  
    24 * $post_string = "app=socket&version=beta";  
    25 * request_by_socket('facebook.cn','/restServer.php',$post_string);  
    26 */   
    27 function request_by_socket($remote_server,$remote_path,$post_string,$port = 80,$timeout = 30){   
    28     $socket = fsockopen($remote_server,$port,$errno,$errstr,$timeout);   
    29      if (!$socket) die("$errstr($errno)");   
    30       
    31     fwrite($socket,"POST $remote_path HTTP/1.0\r\n");   
    32     fwrite($socket,"User-Agent: Socket Example\r\n");   
    33     fwrite($socket,"HOST: $remote_server\r\n");   
    34     fwrite($socket,"Content-type: application/x-www-form-urlencoded\r\n");   
    35     fwrite($socket,"Content-length: ".strlen($post_string)+8."\r\n");   
    36     fwrite($socket,"Accept:*/*\r\n");   
    37     fwrite($socket,"\r\n");   
    38     fwrite($socket,"mypost=$post_string\r\n");   
    39     fwrite($socket,"\r\n");   
    40       
    41     $header = "";   
    42      while ($str = trim(fgets($socket,4096))) {   
    43         $header.=$str;   
    44      }   
    45       
    46     $data = "";   
    47      while (!feof($socket)) {   
    48         $data .= fgets($socket,4096);   
    49      }   
    50       
    51      return $data;   
    52 }  
    53   
    54 /**   
    55 * Curl版本   
    56 * 使用方法:   
    57 * $post_string = "app=request&version=beta";   
    58 * request_by_curl('http://facebook.cn/restServer.php',$post_string);   
    59 */   
    60 function request_by_curl($remote_server,$post_string){   
    61     $ch = curl_init();   
    62     curl_setopt($ch,CURLOPT_URL,$remote_server);   
    63     curl_setopt($ch,CURLOPT_POSTFIELDS,'mypost='.$post_string);   
    64     curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);   
    65     curl_setopt($ch,CURLOPT_USERAGENT,"Jimmy's CURL Example beta");   
    66     $data = curl_exec($ch);   
    67     curl_close($ch);   
    68      return $data;   
    69 }  
    

      

  • 相关阅读:
    bzoj3505 数三角形 组合计数
    cogs2057 殉国 扩展欧几里得
    cogs333 荒岛野人 扩展欧几里得
    bzoj1123 BLO tarjan求点双连通分量
    poj3352 road construction tarjan求双连通分量
    cogs1804 联合权值 dp
    cogs2478 简单的最近公共祖先 树形dp
    cogs1493 递推关系 矩阵
    cogs2557 天天爱跑步 LCA
    hdu4738 Caocao's Bridge Tarjan求割边
  • 原文地址:https://www.cnblogs.com/xiangxiaodong/p/2681639.html
Copyright © 2011-2022 走看看