码迷,mamicode.com
首页 > Web开发 > 详细

php模拟http请求的方法

时间:2016-09-11 10:18:21      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:

我在这里终结了三种方法

第一种方法: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=@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请求的方法

标签:

原文地址:http://www.cnblogs.com/myvic/p/5813997.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!