PHP发送HTTP请求

1. 使用file_get_contents
function fileHttpRequest($data, $url, $method='POST')
{
    $data = http_build_query($data); // http_build_query将key=>value的数组转变为url字符串
    $opts = array(
        'http' => array(
            'method' => $method,
            'header' => 'Content-type: application/x-www-form-urlencodedrn' . 'Content-Length: ' . strlen($data) . '\r\n',
            'content' => $data
        )
    );
    $context = stream_context_create($opts); // stream_context_create创建并返回一个文本数据流并应用各种选项
    return file_get_contents($url, false, $context);
}
2. 使用fsockopen函数, 使用fsockopen需打开 PHP.ini 中的 allow_url_fopen 选项
/**fsockopen 抓取页面
 * @parem $url 网页地址 host 主机地址
 * @parem $port 网址端口 默认80
 * @parem $t 脚本请求时间 默认30s
 * @parem $method 请求方式 get/post
 * @parem $data 如果单独传数据为 post 方式
 * @return string
 * */
function sock_data($url,$t=30,$method='get',$data=null)
{
    $info = parse_url($url);//array ( 'scheme' => 'http', 'host' => 'localhost', 'port' => 1111, 'path' => '/index', )
    // 判断是否有数据
    if(isset($data) && !empty($data))
    {
        $query = http_build_query($data); // 数组转url 字符串形式
    } else {
        $query=null;
    }
    // 如果用户的$url "http://www.manongjc.com/";  缺少 最后的反斜杠
    if(!isset($info['path']) || empty($info['path']))
    {
        $info['path'] = "/index";
    }
    // 判断端口
    $port = isset($info['port']) ? $info['port'] : 80;
    // 打开链接
    $fp = fsockopen($info["host"], $port, $errno, $errstr,$t);
    // 判断 请求方式
    if($method=='post') {
        $head = "POST ".$info['path']." HTTP/1.0".PHP_EOL;
    } else {
        $head = "GET ".$info['path']."?".$query." HTTP/1.0".PHP_EOL;
    }

    $head .= "Host: ".$info['host'].PHP_EOL; // 请求主机地址
    $head .= "Referer: ".$url.PHP_EOL;
    if(isset($data) && !empty($data) && ($method=='post')) {
        $head .= "Content-type: application/x-www-form-urlencoded".PHP_EOL;
        $head .= "Content-Length: ".strlen(trim($query)).PHP_EOL;
        $head .= PHP_EOL;
        $head .= trim($query);
    } else {
        $head .= PHP_EOL;
    }
    //print_r($head);die();
    $write = fputs($fp, $head); //写入文件(可安全用于二进制文件)。 fputs() 函数是 fwrite() 函数的别名
    $res = '';
    while (!@feof($fp))
    {
        $res .= @fread($fp,4096);
    }
    fclose($fp);
    print_r($res);
}
// 函数调用
$url = "http://localhost:1111/index"; #url 地址必须 http://xxxxx
$port=1111;
$t=30;
$data = array(
    'a'=>'123',
    'b'=>'456'
);
sock_data($url,$t,'get',$data);
3. 使用curl库发送http请求
/**curl发送http请求
 * @param $url string 访问地址,get方法时加?
 * @param $method string get/post
 * @param $data array
 */
function http_request($url, $method='get', $data=null) {
    //第一步:创建curl
    $ch = curl_init();
    //第二步:设置curl
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //禁止服务器端校检SSL证书
    //判断$data数据是否为空
    if($method == 'post' && !empty($data)) {
        //模拟发送POST请求
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //以文档流的形式返回数据
    //第三步:执行curl
    $output = curl_exec($ch);
    //第四步:关闭curl
    curl_close($ch);
    //把$output当做返回值返回
    return $output;
}

4. 注释

//(1) parse_url, parse_str与http_build_query的使用
// 使用parse_url将url转为数组
$str0 = "http://mytest.me/tools?a=123&b=哈哈哈";
$arr0 = parse_url($str0); // array ( 'scheme' => 'http', 'host' => 'mytest.me', 'path' => '/tools', 'query' => 'a=123&b=456', )
// 使用parse_str将query变为key=>value的数组
parse_str($arr0['query'],$arr1); // array ( 'a' => '123', 'b' => '456', )

//(2) 使用http_build_query将key=>value的数组转变为url字符串, 会自动进行urlencode处理
$url = http_build_query($arr1); //a=123&b=%E5%93%88%E5%93%88%E5%93%88

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值