Reputation: 4169
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;
}
Upvotes: 0
Views: 960
Reputation: 26074
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;
}
Upvotes: 1