duanjiaolia97750 2011-08-09 18:17
浏览 206
已采纳

尝试读取网页时,file_get_contents偶尔会返回空字符串

I am attempting to load a webpage with file_get_contents(), and am running into problems.

Basically, my web app will use the youtube data api to search for videos based on a user's query, and then read each video's youtube page to find information not provided by the api. I use file_get_contents() to read the youtube pages and then load the text into a DOM parser. Most of the time, this works like a charm. However, on occasion, I'll get warnings saying that file_get_contents() returned an empty string (not for all videos, only for some of them). I know that the url I am providing is correct, because I echo a link to that url and it works as expected. I'll refresh the page, reopen my browser, switch to a different browser, etc. but nothing will work. Then, I'll leave the thing for an hour or two, come back to it, and it will magically work again!

Here is a snippet of my code:

function processNext($int) {
    // this function processes the next $int videos from the youtube data api response ($xmlDoc)
    global $xmlDoc;
    $begin = count($_SESSION["results"]) - $_SESSION["start"] + 1;
        /* $_SESSION["results"] is the array of already-processed videos
           $_SESSION["start"] and $_SESSION["end"] are the indexes of the first and last videos in the $xmlDoc
        */
    $end = count($_SESSION["results"]) - $_SESSION["start"] + $int;

    for ($i = $begin; $i <= $end; $i++) {
        $video = $xmlDoc->entry[$i];
        $doc = new DOMDocument();
        $doc->strictErrorChecking = FALSE;
        libxml_use_internal_errors(true);

        // this is the line that is causing me problems
        $doc->loadHTML(file_get_contents(getWatchURL(getVidID($video->id)))); 

        $doc = $doc->documentElement;

               // then, do some processing on the $doc

    }
}

Is it possible that file_get_contents() is timing out? Is cURL a better tool for what I am doing?

UPDATE: I get the same results with cURL.

  • 写回答

1条回答 默认 最新

  • doubao7287 2011-08-09 19:41
    关注

    Look at $http_response_header. You can check the status code of the request. If it's anything other than 200, something has likely gone wrong. More about status codes.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站