首页 > 后端开发 > php教程 > php采集 curl

php采集 curl

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
发布: 2016-06-06 20:26:30
原创
1231 人浏览过

使用curl采集,$url = 'http://kakaku.com/pc/';请问怎么修改下面的代码?

 function GetContent($url = "", $post_data = '', $method = 'POST', $timeout = 500) {
        if (is_array($post_data)) {
            $post_data = http_build_query($post_data);
        }
        if ($method == 'GET') {
            $url = $url . '?' . ltrim($post_data, '?');
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) \r\n Accept: */*'));
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if ($method == 'POST') {
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        }
        $content = curl_exec($ch);
        $response = curl_getinfo($ch);
        return $content;
    }
登录后复制
登录后复制

回复内容:

使用curl采集,$url = 'http://kakaku.com/pc/';请问怎么修改下面的代码?

 function GetContent($url = "", $post_data = '', $method = 'POST', $timeout = 500) {
        if (is_array($post_data)) {
            $post_data = http_build_query($post_data);
        }
        if ($method == 'GET') {
            $url = $url . '?' . ltrim($post_data, '?');
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) \r\n Accept: */*'));
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if ($method == 'POST') {
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        }
        $content = curl_exec($ch);
        $response = curl_getinfo($ch);
        return $content;
    }
登录后复制
登录后复制

指定 第三个参数 的值为 GET 就可以了.

php采集 curl

你是要采集get还是post请求的内容?

若是采集首页直接
echo file_get_contents('http://kakaku.com/pc/'); 即可

相关标签:
php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
怎么学好php
来自于 1970-01-01 08:00:00
0
0
0
PHP扩展intl
来自于 1970-01-01 08:00:00
0
0
0
php数据获取?
来自于 1970-01-01 08:00:00
0
0
0
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板