2

这是我的代码:

function get_data($url)
{
$ch = curl_init();
$timeout = 15;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_USERAGENT, random_user_agent());
$data = curl_exec($ch);
curl_close($ch);
return $data;
}

//Grab HTML
$urllist = fopen("links.txt", "r+");
for ($j = 0; $j <= 50; $j++)
{
$post = rtrim(fgets($urllist));
echo $post;
$html = get_data($post);
echo $html;

问题:当我使用时,我get_data("http://url.com")在 html 中获得了正确的数据。但是当我使用变量传递 url 时,$html什么也不返回。

$post 在我检查时拥有正确的网址。这不是正确的使用方法吗get_data($post);

卷曲信息给出:

我明白了:

array(20) { 
["url"]=> string(68) "http://secret-url.com" 
["content_type"]=> string(9) "text/html" 
["http_code"]=> int(301) 
["header_size"]=> int(255) 
["request_size"]=> int(340) 
["filetime"]=> int(-1) 
["ssl_verify_result"]=> int(0) 
["redirect_count"]=> int(0) 
["total_time"]=> float(0.095589) 
["namelookup_time"]=> float(0.012224) 
["connect_time"]=> float(0.049399) 
["pretransfer_time"]=> float(6.5E-5) 
["size_upload"]=> float(0) 
["size_download"]=> float(0) 
["speed_download"]=> float(0) 
["speed_upload"]=> float(0) 
["download_content_length"]=> float(0) 
["upload_content_length"]=> float(0) 
["starttransfer_time"]=> float(0.095534) 
["redirect_time"]=> float(0) 
}
4

2 回答 2

2

试试这个代码。

function get_data($url)
{
    $ch = curl_init();
    $timeout = 15;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_USERAGENT, random_user_agent());

    // Edit: Follow redirects
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 

    $data = curl_exec($ch);
    var_dump(curl_getinfo($ch));
    curl_close($ch);
    return $data;
}

//Grab HTML
$urllist = fopen("links.txt", "r+");
for ($j = 0; $j <= 50; $j++)
{
    if($post = rtrim(fgets($urllist)))
    {
        echo $post;
        echo get_data($post);
    }
    else
    {
        echo "No URL provided!";
    }

    echo "\n<hr>\n";
}
于 2012-05-02T19:11:05.983 回答
0

$html = file_get_contents($url);了吗?正如记录显示的那样,它没有=)

编辑以合理的答案总结对话;

将您的 curl 更改为以下,包含 FOLLOWLOCATION 指令,可选择使用 MAXREDIRS 约束 curl

function get_data($url) {
    $ch = curl_init();
    $timeout = 15;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    // follow Location: newurl.tld - i.e. HTTP 30X status codes
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    // give up follow location if circular after 5 tries
    curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_USERAGENT, random_user_agent());
    $data = curl_exec($ch);
    // if in doubt, whats going on, look through info of curl_getinfo($ch);
    // var_dump(curl_getinfo($ch));
    curl_close($ch);
    return $data;
}
//Grab HTML
$urllist = fopen("links.txt", "r+");
for ($j = 0; $j <= 50; $j++) {
    $post = rtrim(fgets($urllist));
    echo $post;
    $html = get_data($post);
    echo $html;
}

可选地,由于您似乎不止一次这样做,返回到您的 links.txt 页面 - 设置一个 cookie 容器,允许访问知道您以前去过那里 - 并在连续运行中重复使用该信息

// filehandle, writeable by httpd user:
$cookie_file = "/tmp/cookie/cookie1.txt";
// set request to parse cookies and send them with corresponding host requests
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
// set response cookies to be saved
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
于 2012-05-02T18:44:36.400 回答