国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 網(wǎng)站 > 幫助中心 > 正文

php發(fā)送post請(qǐng)求的三種方法

2024-07-09 22:41:52
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

方法一:

/** * 發(fā)送post請(qǐng)求 * @param string $url 請(qǐng)求地址 * @param array $post_data post鍵值對(duì)數(shù)據(jù) * @return string */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 // 超時(shí)時(shí)間(單位:s)    )  );  $context = stream_context_create($options);  $result = file_get_contents($url, false, $context);  return $result;}//使用方法$post_data = array(  'username' => 'stclair2201',  'password' => 'handan');send_post('//www.jb51.net', $post_data);

方法二:Socket版本

<?php/** * Socket版本 * 使用方法: * $post_string = "app=socket&version=beta"; * request_by_socket('chajia8.com', '/restServer.php', $post_string); */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, "mypost=$post_string");  fwrite($socket, "");  $header = "";  while ($str = trim(fgets($socket, 4096))) {    $header .= $str;  }  $data = "";  while (!feof($socket)) {    $data .= fgets($socket, 4096);  }  return $data;}?>

方法三:Curl版本

<?php/** * Curl版本 * 使用方法: * $post_string = "app=request&version=beta"; * request_by_curl('//www.jb51.net/restServer.php', $post_string); */function request_by_curl($remote_server, $post_string) {  $ch = curl_init();  curl_setopt($ch, CURLOPT_URL, $remote_server);  curl_setopt($ch, CURLOPT_POSTFIELDS, 'mypost=' . $post_string);  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  curl_setopt($ch, CURLOPT_USERAGENT, "jb51.net's CURL Example beta");  $data = curl_exec($ch);  curl_close($ch);  return $data;}?>

下面是其他網(wǎng)友的方法:

class Request{  public static function post($url, $post_data = '', $timeout = 5){//curl    $ch = curl_init();    curl_setopt ($ch, CURLOPT_URL, $url);    curl_setopt ($ch, CURLOPT_POST, 1);    if($post_data != ''){      curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);    }    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);     curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);    curl_setopt($ch, CURLOPT_HEADER, false);    $file_contents = curl_exec($ch);    curl_close($ch);    return $file_contents;  }  public static function post2($url, $data){//file_get_content        $postdata = http_build_query(      $data    );        $opts = array('http' =>           array(             'method' => 'POST',             'header' => 'Content-type: application/x-www-form-urlencoded',             'content' => $postdata           )    );        $context = stream_context_create($opts);    $result = file_get_contents($url, false, $context);    return $result;  }  public static function post3($host,$path,$query,$others=''){//fsocket    $post="POST $path HTTP/1.1/r/nHost: $host/r/n";    $post.="Content-type: application/x-www-form-";    $post.="urlencoded/r/n${others}";    $post.="User-Agent: Mozilla 4.0/r/nContent-length: ";    $post.=strlen($query)."/r/nConnection: close/r/n/r/n$query";    $h=fsockopen($host,80);    fwrite($h,$post);    for($a=0,$r='';!$a;){        $b=fread($h,8192);        $r.=$b;        $a=(($b=='')?1:0);      }    fclose($h);    return $r;  }}
發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 浙江省| 海口市| 抚松县| 庆安县| 湟源县| 伊吾县| 普兰县| 阳西县| 象山县| 亚东县| 榆社县| 敦煌市| 本溪市| 惠东县| 宣恩县| 泉州市| 桦川县| 平泉县| 鞍山市| 万安县| 阿巴嘎旗| 阿拉善左旗| 海丰县| 隆德县| 伊金霍洛旗| 玉林市| 广饶县| 石景山区| 大悟县| 老河口市| 班玛县| 马山县| 邵阳县| 西安市| 沅陵县| 余干县| 平顶山市| 临泽县| 霍邱县| 泰宁县| 舞钢市|