PHP用curl请求网址的code码

/**
 * http测试
 * 注:PHP版本5.2以上才支持CURL_IPRESOLVE_V4
 * @param $url 网站域名
 * @param $type 网站访问协议
 * @param $ipresolve 解析方式
 */
public function web_http($url,$type,$ipresolve) {
    //设置Header头
    $header[] = "Accept: application/json";
     $header[] = "Accept-Encoding: gzip";
    $httptype = function_exists('curl_init');
    if (!$httptype) {
      $html = file_get_contents($url);
    } else {
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      //输出头信息
      curl_setopt($ch, CURLOPT_HEADER, 1);
      //递归访问location跳转的链接,直到返回200OK
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
      //不对HTML中的BODY部分进行输出
      curl_setopt($ch, CURLOPT_NOBODY, 1);
      //将结果以文件流的方式返回,不是直接输出
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      //以IPv4/IPv6的方式访问
      if($ipresolve=='ipv6') {
        curl_setopt($ch,CURLOPT_IPRESOLVE,CURL_IPRESOLVE_V6);
      }else{
        curl_setopt($ch,CURLOPT_IPRESOLVE,CURL_IPRESOLVE_V4);
      }
      //添加HTTP header头采用压缩和GET方式请求
      curl_setopt( $ch, CURLOPT_HTTPHEADER, $header );
      curl_setopt($ch,CURLOPT_ENCODING , "gzip");
      curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
      //清除DNS缓存
      curl_setopt($ch,CURLOPT_DNS_CACHE_TIMEOUT,0);
      //设置连接超时时间
      curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,15);
      //设置访问超时
      curl_setopt($ch,CURLOPT_TIMEOUT,50);
      //设置User-agent
      curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11');
      if($type=="https") {
          //不对认证证书来源的检查
          curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
          //从证书中检查SSL加密算法是否存在 
          curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);
      }
      //执行Curl操作
      $html = curl_exec($ch);
      //获取一个cURL连接资源句柄的信息(获取最后一次传输的相关信息)
      $info = curl_getinfo($ch);
      curl_close($ch);
    }
    return $info;
  }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值