php学习_如何利用php语言模拟发送get请求?

    作者:Comet更新于: 2015-06-26 09:59:29

    Web开发
    如何利用php语言模拟发送get请求?课课家小编给学者们分享以下php学习的几种方法总结。
     
    php学习模拟发送get请求
     
    (1)php 通过 file_get_contents 模拟发送 get 请求

    12$url='http://www.phpernote.com/php-function/654.html';
    3$re=file_get_contents($url);
    4print_r($re);

    (2)php 通过 curl 模拟发送 get 请求

    12$ch=curl_init('http://www.phpernote.com/php-function/651.html');
    3curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    4curl_setopt($ch,CURLOPT_BINARYTRANSFER,true);
    5$output=curl_exec($ch);
    6$fh=fopen("out.html",'w');
    7fwrite($fh,$output);
    8fclose($fh);

    (3)php 通过 fsocket 模拟发送 get 请求

    0102function sock_get($url){ 
    03    $info=parse_url($url);
    04    $fp=fsockopen($info["host"],80,$errno,$errstr,3);
    05    $head="GET ".$info['path']."?".$info["query"]." HTTP/1.0\r\n";
    06    $head.="Host: ".$info['host']."\r\n";
    07    $head.="\r\n";
    08    $write=fputs($fp,$head);
    09    while(!feof($fp)){ 
    10        $line=fgets($fp);
    11        echo $line."
    ";
    12    }
    13}
    该函数的使用方法如下:$url='http://www.phpernote.com/jquery-effects/650.html?page=2';
    echo "以下是GET方式的响应内容:
    ";
    sock_get($url); 
    更多PHP学习入门教程的相关知识,可登陆课课家网络教学平台进行查询观看视频教程!
php 更多推荐

课课家教育

未登录