drbz99867 2014-05-04 03:53
浏览 89

如何在循环中运行file_get_content?

I'm trying to loop it. But when executing this script:

    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;
    }

The reponse I get in the file_get_contents() doesn't match the url called (the parameters changes during the loop of course, its like a pagination, but I always get the first page for no reason.). Even if the URL is ok, it doesn't seem to call this page and always returns the same results. But when I copy and paste the URL from the what I get in the echo to my browser search/url bar, I get the right results.

I have the feeling I'm missing something with file_get_contents() maybe to clear the previous call or something.

EDIT: This is the 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条回答 默认 最新

  • donglin5770 2014-05-04 04:06
    关注

    Without providing fuller code, hard to know exactly what is happening. But I have two ideas based on past experience.

    First, perhaps the server is simply unable to handle too many requests at once. So I would suggest adding a sleep() setting in your script to pause between request to give the server a chance to catch up.

    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;
    }
    

    The other idea is perhaps the server you are trying to connect to is blocking curl requests? What happens if you go to the command line and type the following"

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

    Or even check the headers with the curl -I option like this:

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

    EDIT: Looking at your makeURL() function shows me another glaring issue. Pretty sure you should be using urlencode() on the values.

    This is how I would recode your function:

    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;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大