• php curl请求实例


    curl post json 数据

        $data = array("name" => "Hagrid", "age" => "36");                                                                      
        $data_string = json_encode($data);       
        $ch = curl_init('http://api.local/rest/users');        
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                            
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);  
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(                   
            'Content-Type: application/json',  
            'Content-Length: ' . strlen($data_string))           
        );                                                                                                                     
        $result = curl_exec($ch);  

    curl post 自定义环境变量 

    设置 CURLOPT_HTTPHEADER

    【$_SERVER['PHP_AUTH_USER'] 是由服务器控制产生的变量,你不能给它赋值】
    function curlRequest($url, $data_string) {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(        
            'bing:bingbing')
        );
        $result = curl_exec($ch);
        return $result;
    }

    curl post xml数据

    $header[] = "Content-type: text/xml"; //定义content-type为xml
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);
    $response = curl_exec($ch);
    if (curl_errno($ch)) {
            print curl_error($ch);
    }
    curl_close($ch);

    "<xml>
    <ToUserName><![CDATA[%s]]></ToUserName>
    <FromUserName><![CDATA[%s]]></FromUserName>
    <CreateTime>%s</CreateTime>
    <MsgType><![CDATA[%s]]></MsgType>
    <Content><![CDATA[%s]]></Content>
    </xml>"

    /**
     * curl获取指定路径内容
     */
    function curlRequest($url, $method = '', $postdata = '', $isjson=0) {
        $ch = curl_init();
        $post = $method && strtolower($method) == 'post' ? 1 : 0;
        curl_setopt($ch, CURLOPT_URL, $url);
        if($post) {
            curl_setopt($ch, CURLOPT_POST, $post);
            if($postdata) {
               if(!$isjson) { 
                    $postdata = http_build_query($postdata);
               }
               curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
            }
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
        curl_setopt($ch, CURLOPT_TIMEOUT, 80);
        if($isjson) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type:application/json',
                'Content-Length:'. strlen($postdata)
            ));
        }    
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)");
        $data = curl_exec($ch);
        $datainfo = curl_getinfo($ch);
        curl_close($ch);
        if($datainfo['http_code'] == 200) {
            return array($data, $datainfo);
        } else {
            return array();
        }
    }

     curl 模拟百度蜘蛛访问

    $url = 'xxxxxxxx?' . time();
    echo curl_get($url);
    /**
     * 获取内容
     */
    function curl_get($url) {
       $header = array(
           'CLIENT-IP:' . mt_rand(1, 255) . '.' . mt_rand(1, 255) . '.' . mt_rand(1, 255) . '.'. mt_rand(1, 255) . '',
           'X-FORWARDED-FOR:' . mt_rand(1, 255) . '.' . mt_rand(1, 255) . '.' . mt_rand(1, 255) . '.' . mt_rand(1, 255) . '',
       );
       $ch = curl_init(); //初始化curl
       curl_setopt($ch, CURLOPT_URL, $url); //设置链接
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //设置是否返回信息
       curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //设置HTTP头
       curl_setopt($ch, CURLOPT_HEADER, true);
       curl_setopt($ch, CURLOPT_TIMEOUT,60); //超时设置
       curl_setopt($ch, CURLOPT_REFERER, 'http://www.baidu.com');
       curl_setopt($ch, CURLOPT_USERAGENT, 'Baiduspider+(+http://www.baidu.com/search/spider.htm)');
       curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
       $content = curl_exec($ch); //接收返回信息
       return $content;
    }

    curl 获取百度搜索关键词内容

    $gpc = stf%3D1434211200%2C1434297599|stftype%3D2      时间范围,无此参数表示全部数据
    $pos = 0,10,20,...10n 分页
    wd 关键字 
    $url = 'http://www.baidu.com/s?wd=site%3Axxx.xxx.com%20%E8%B5%8C&gpc=' . $gpc . '&cl=3&pn=' . $pos; 
  • 相关阅读:
    spark sql 性能调优
    google c++ 规范
    idea
    Ubuntu16.04-hadoop3 安装与配置
    NewRelic 性能监测工具
    关于 g++ link
    vue 实现 多个 数字滚动增加动效
    mac 电脑安装express、npm…… 报 ‘Missing write access to /usr/local/lib/node_modules’错误解决办法
    vue + mock.js 简单使用
    vue去掉地址栏# 方法
  • 原文地址:https://www.cnblogs.com/bandbandme/p/3517236.html
Copyright © 2020-2023  润新知