標籤:
我在這裡終結了三種方法
第一種方法: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請求的方法