php類比http請求的方法

來源:互聯網
上載者:User

標籤:

我在這裡終結了三種方法

第一種方法:fsockopen

$flag = 0; $post = ‘‘; $errno = ‘‘; $errstr = ‘‘; //要post的資料$argv = array(    ‘username‘=>‘vic‘,    ‘content‘=>‘how are you , my friend?‘);//構造要post的字串foreach ($argv as $key=>$value) {    if ($flag!=0) {        $post .= "&";        $flag = 1;    }    $post.= $key."="; $post.= urlencode($value);    $flag = 1;    }    $length = strlen($post);     //建立socket串連   $fp = fsockopen("www.test.com",80,$errno,$errstr,10) or exit($errstr."--->".$errno);    //構造post請求的頭    $header  = "POST /mysql.php HTTP/1.1\r\n";    $header .= "Host:www.test.com\r\n";    $header .= "Referer:/index.php\r\n";    $header .= "Content-Type: application/x-www-form-urlencoded\r\n";    $header .= "Content-Length: ".$length."\r\n";    $header .= "Connection: Close\r\n\r\n";    //添加post的字串    $header .= $post."\r\n";        //發送post的資料   fputs($fp,$header);    $inheader = 1;    while (!feof($fp)) {        $line = fgets($fp,1024); //去除請求包的頭只顯示頁面的返回資料        if ($inheader && ($line == "\n" || $line == "\r\n")) {                         $inheader = 0;        }        if ($inheader == 0) {          echo $line;        }    }fclose($fp);$html=file_get_contents("http://www.baidu.com");echo $html;

第二種方法:stream_context_create()

$data=array(‘nickname‘=>‘yonghuming‘,‘Email‘=>‘假的‘);$data=http_build_query($data);//var_dump($data);$strlen=strlen($data);$opts=array(‘http‘=>array(        ‘method‘=>‘POST‘,        ‘header‘=>"Content-Type:application/x-www-form-urlencoded"."\r\n"."Content-Length:".$strlen."\r\n"."User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36"."\r\n"."Referer:http://www.test.com/stream.php"."\r\n",        ‘content‘=>$data            ));$context=stream_context_create($opts);//var_dump($opts);$html[email protected]file_get_contents("http://www.test.com/mysql.php",false,$context);echo $html;

第三種方法:curl

 

//初始化 $ch=curl_init();//設定選項curl_setopt($ch, CURLOPT_URL, "http://www.test.com/post.php");curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//檔案流輸出,不是直接輸出curl_setopt($ch, CURLOPT_HEADER, 0);//啟用時,標頭檔資訊作為資料流輸出$data=array(‘nickname‘=>‘yonghuming‘,‘Email‘=>‘假的‘);$data=http_build_query($data);//var_dump($data);$strlen=strlen($data);//curl已經類比了頭部資訊,如不是特需要求,可以不需要的/*$header=array("Content-Type:application/x-www-form-urlencoded","Content-Length:".$strlen,"User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36","Referer:http://www.test.com/stream.php");curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//類比頭部資訊*///設定post請求curl_setopt($ch, CURLOPT_POST, 1);curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//設定cookie$cookie_jar = dirname(__FILE__)."/pic.cookie";curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);// 存放Cookie資訊的檔案名稱  curl_setopt($ch,CURLOPT_COOKIEFILE,$cookie_jar); // 讀取上面所儲存的Cookie資訊 curl_setopt($ch,CURLOPT_COOKIE,‘age=12‘);//單獨設定cookie,如何不用上面的兩個方法 //3.執行$output=curl_exec($ch);//4.釋放curl_close($ch);//var_dump($output);echo $output;

post.php檔案

echo ‘<pre>‘;echo "this is the data posted";setcookie(‘name‘,‘vic‘);print_r($_POST);print_r($_COOKIE);echo ‘</pre>‘;

結果

this is the data postedArray(    [nickname] => yonghuming    [Email] => 假的)Array(    [name] => vic    [age] => 12)
總結:php請求基本上此三種方法就可以了,此只是非常簡單的入門,複雜的等以後再來總結吧。

php類比http請求的方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.