如何在循环中运行file_get_content?

时间:2014-05-04 03:53:32

标签: php curl file-get-contents

我正在尝试循环它。但是在执行这个脚本时:

    for ( $page_number = 1; $page_number <= $pages_count ; $page_number++ )
    {
        $url = $this->makeURL( $limit, $category->getSources()[0]->getSourceId(), $subCat->getSources()[0]->getSourceId() );
        echo $url."</br>"; // Returns the correct URL
        $json = json_decode( file_get_contents( $url ) );

        echo "<pre>";
        print_r( $json ); //Only return the proper date the first time. Then its always the same whatever the url
        echo "</pre>";

        //$this->insert( $json, $subCat );
        $limit = $limit + 10;
    }

我在file_get_contents()中得到的响应与被调用的url不匹配(当然,参数在循环期间发生变化,就像分页一样,但我总是无缘无故地获得第一页。)。即使URL正常,它似乎也不会调用此页面并始终返回相同的结果。但是,当我将回显的URL复制并粘贴到我的浏览器搜索/网址栏中时,我得到了正确的结果。

我感觉我错过了file_get_contents()的某些内容,可能是为了清除之前的电话或其他内容。

编辑:这是makeURL()

public function makeURL( $limit, $mainCat, $subCat )
{
    $pagi_start = $limit;
    $url = "http://some.api/V3/event_api/getEvents.php?"
            . "&cityId="    . "39"
            . "&subcat="    . $subCat
            . "&cat="       . $mainCat
            . "&link="      . "enable"
            . "&tags="      . "enable"
            . "&strip_html=". "name,description"
            . "&moreInfo="  . "popularity,fallbackimage,artistdesc"
            . "&tz="        . "America/Los_ Angeles"
            . "&limit="     . $pagi_start.",10";

    return $url;
}

1 个答案:

答案 0 :(得分:1)

如果没有提供更全面的代码,很难确切知道发生了什么。但根据过去的经验,我有两个想法。

首先,也许服务器根本无法一次处理太多请求。因此,我建议在脚本中添加sleep()设置,以便在请求之间暂停,以便让服务器有机会赶上。

for ( $page_number = 1; $page_number <= $pages_count ; $page_number++ )
{
    $url = $this->makeURL( $limit, $category->getSources()[0]->getSourceId(), $subCat->getSources()[0]->getSourceId() );
    echo $url."</br>";
    $json = json_decode( file_get_contents( $url ) );

    // Adding a 'sleep()' command with a 10 second pause.
    sleep(10);

    echo "<pre>";
    print_r( $json );
    echo "</pre>";

    //$this->insert( $json, $subCat );
    $limit = $limit + 10;
}

另一个想法可能是您尝试连接的服务器阻止了卷曲请求?如果您转到命令行并键入以下“

,会发生什么
curl "http://some.api/V3/event_api/getEvents.php?[all parameters here]"

甚至可以使用curl -I选项检查标题,如下所示:

curl -I "http://some.api/V3/event_api/getEvents.php?[all parameters here]"

编辑:查看您的makeURL()功能,向我展示了另一个明显的问题。很确定你应该对值使用urlencode()

这就是我重新编码你的功能的方法:

public function makeURL( $limit, $mainCat, $subCat )
{
    $pagi_start = $limit;

    // Set the param values.
    $param_array = array();
    $param_array['cityId'] = 39;
    $param_array['subcat'] = $subCat;
    $param_array['cat'] = $mainCat;
    $param_array['link'] = "enable";
    $param_array['tags'] = "enable";
    $param_array['strip_html'] = "name,description";
    $param_array['moreInfo'] = "popularity,fallbackimage,artistdesc";
    $param_array['tz'] = "America/Los_ Angeles";
    $param_array['limit'] = $pagi_start . ",10";

    // Now roll through the param values urlencode them.
    $param_array_urlencoded = array();
    foreach($param_array as $param_key => $param_value) {
      $param_array_urlencoded[$param_key] = urlencode($param_value);
    }

    // Create the final param array.
    $param_array_final = array();
    foreach($param_array_urlencoded as $final_param_key => $final_param_value) {
      $param_array_final[] = $final_param_key . "=" . $final_param_value;
    }

    // Create the final URL with the imploded `$param_array_final`.
    $url = "http://some.api/V3/event_api/getEvents.php?" . implode("&", $param_array_final) ;

    return $url;
}