我正在尝试循环它。但是当执行这个脚本时:
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是正确的,它似乎也不会调用这个页面,并且总是返回相同的结果。但是,当我从我得到的echo中复制并粘贴URL到浏览器的搜索/URL栏时,我得到了正确的结果。
我感觉我错过了file_get_contents()
的一些东西,可能是为了清除之前的呼叫或其他什么。
EDIT:这是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;
}
如果不提供更完整的代码,很难确切地知道发生了什么。但根据过去的经验,我有两个想法。
首先,可能服务器无法同时处理太多请求。所以我建议在你的脚本中添加一个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请求?如果转到命令行并输入以下"
"会发生什么?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;
}