簡體   English   中英

為什么在PHP中使用cURL在PC和VPS中獲得不同的結果?

[英]Why I get different results in my pc and vps with cURL in PHP?

我想從imooc.com使用PHP中的cURL獲取用戶信息。 因此,我編寫了一個小程序來解決它。我的方法是訪問www.imooc.com/space/profile/uid/{$uid} / {$uid}{$uid}是自動遞增的)並將數據保存到數據庫中。 但有時我會遇到302 redirect 我發現imooc.com有兩個重定向站點,例如,當您訪問www.imooc.com/space/profile/uid/110073www.imooc.com/space/profile/uid/212328 ,您會發現它們已重定向到不同的站點( www.imooc.com/error/noexistswww.imooc.com/course/list )。

在我的PC和www.imooc.com/error/noexists ,當重定向到www.imooc.com/error/noexists時,我可以得到相同的結果,但是,如果重定向到另一個站點,我的PC可以完美地解決它,而我的VPS卻什么也沒做,但不起作用。

我該如何解決這個問題? 這是我的代碼:

// connect to DB
$dsn = "mysql:host=localhost;dbname=tools";
$link = new PDO($dsn, 'root', 'root', array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8';"));

$sql = "SELECT MAX(uid) FROM imooc";
$stmt = $link->prepare($sql);
$stmt->execute();
$lastUid = $stmt->fetch()[0];

$uid = $lastUid+1;

// I execute this script using cron every minute, so I use loop to make sure it will get 40 sites
while(($lastUid + 40) >= $uid){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://www.imooc.com/space/profile/uid/{$uid}");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    $output = curl_exec($ch);

    curl_close($ch);

    $str = strstr($output, "的個人詳情-慕課網</title>", true);
    if ($str === false) {
        $sql = "INSERT INTO imooc(uid,username, site, learntime, experience, usersex, usercity) VALUES(:uid, :username, :site, :learntime, :experience, :usersex, :usercity)";
        $stmt = $link->prepare($sql);
        $stmt->bindParam(':uid', $uid);
        $stmt->bindValue(':username', '沒有找到此人');
        $stmt->bindValue(':site', '沒有該人的職業信息');
        $stmt->bindValue(':learntime', '沒有找到該人的學習時間');
        $stmt->bindValue(':experience', '沒有找到該人的學習經驗');
        $stmt->bindValue(':usersex', '沒有找到該人的性別');
        $stmt->bindValue(':usercity', '沒有找到該人的城市');
        $stmt->execute();
        $uid++;
        continue;
    }

    // get user-name
    $title = strstr($str, "<title>");
    $name = substr($title, 7);

    // get user-site
    $str = strstr($output, '<span class="user-site">');
    $span = strstr($str, "</span>", true);
    $site = trim(substr($span, 24));

    // get learn-time
    $str = strstr($output, '<p class="mp-num">');
    $span = strstr($str, '</p>', true);
    $learntime = trim(substr($span, 18));

    // get user-experience
    $str = strstr($output, '<span class="mp-num">');
    $span = strstr($str, '</span>', true);
    $experience = trim(substr($span, 21));

    // get user-sex
    $str = strstr($output, '<em >');
    $span = strstr($str, '</em>', true);
    $usersex = trim(substr($span, 5));
    if ($str === false) {
        $usersex = '保密';
    }

    // get user-city
    $str = strstr($output, '<span>所在城市:</span>');
    $span = strstr($str, '</ul>', true);
    $city = strip_tags(substr($span, strlen('<span>所在城市:</span>') ) );
    $usercity = str_replace(' ', '', $city);

    // insert data into database
    $sql = "INSERT INTO imooc(uid,username, site, learntime, experience, usersex, usercity) VALUES(:uid, :username, :site, :learntime, :experience, :usersex, :usercity)";
    $stmt = $link->prepare($sql);
    $stmt->bindParam(':uid', $uid);
    $stmt->bindParam(':username', $name);
    $stmt->bindParam(':site', $site);
    $stmt->bindParam(':learntime', $learntime);
    $stmt->bindParam(':experience', $experience);
    $stmt->bindParam(':usersex', $usersex);
    $stmt->bindParam(':usercity', $usercity);
    $stmt->execute();
    $uid++;
}

我使用XAMPP V3.2.1和5.6.11版本的PHP,我的vps PHP版本也是5.6.11。

如果您只需要從有效頁面(非重定向,現有uid)中獲取內容,則應檢查是否發生了重定向並跳過此類頁面。

為此,請檢查卷曲響應元信息(響應標頭)以獲取響應代碼(http_code)或redirect_url。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM