PHP/MySQL/JSON - Looping through all pages of JSON response - php

I am calling the Crunchbase API and it gives me long response, so long that the response has multiple pages that can be accessed with ?page=# at the end of the api url.
My question is how do I write some code to run the script once and it will go through all of the pages available without me having the change the page number every time I call the script?
Simplified version of my code:
$url = "https://api.url.com/tags/?page=2";
$jsondata = file_get_contents($url);
$array = json_decode($jsondata,true);
var_dump($array);
foreach($array as $key => $value) {
mysql_query(" INSERT into cbcompanies (
`column1`)
VALUES (
'{$value['foo']}') ",$con);
}

If you want to make multiple requests you have to use loops or explicitly get all the pages.
$numberOfPages = 100;
for($i = 1; $i < $numberOfPages; $i++) {
$url = sprintf("https://api.url.com/tags/?page=%d", $i);
// Rest of the code.
}

Related

Need help decoding JSON from Riot API with PHP

As a part of an assignment I am trying to pull some statistics from the Riot API (JSON data for League of Legends). So far I have managed to find summoner id (user id) based on summoner name, and I have filtered out the id's of said summoner's previous (20) games. However now I can't figure out how to get the right values from the JSON data. So this is when I'll show you my code I guess:
$matchIDs is an array of 20 integers (game IDs)
for ($i = 1; $i <= 1; $i++)
{
$this_match_data = get_match($matchIDs[$i], $server, $api);
$processed_data = json_decode($this_match_data, true);
var_dump($processed_data);
}
As shown above my for loop is set to one, as I'm just focusing on figuring out one before continuing with all 20. The above example is how I got the match IDs and the summoner IDs. I'll add those codes here for comparison:
for ($i = 0; $i <= 19; $i++)
{
$temp = $data['matches'][$i]['matchId'];
$matchIDs[$i] = json_decode($temp, true);
}
$data is the variable I get when I pull all the info from the JSON page, it's the same method I use to get $this_match_data in the first code block.
function match_list($summoner_id, $server, $api)
{
$summoner_enc = rawurlencode($summoner);
$summoner_lower = strtolower($summoner_enc);
$curl =curl_init('https://'.$server.'.api.pvp.net/api/lol/'.$server.'/v2.2/matchlist/by-summoner/'.$summoner_id.'?api_key='.$api.'');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
Now to the root of the problem, This is where I put the data I get from the site, so you can see what I am working with. Now by using the following code I can get the first value in that file, the match ID.
echo $processed_data['matchId'];
But I can't seem to lock down any other information from this .json file. I've tried typing stuff like ['region'] instead of ['matchId'] with no luck as well as inserting index numbers like $processed_data[0], but nothing happens. This is just how I get the right info from the first examples and I am really lost here.
Ok, so I think I've figured it out myself. By adding this to the code I can print out the json file in a way more human-friendly way, and that should make it much easier to handle the data.
echo ("<pre>");
var_dump($processed_data);
echo ("</pre>");

How to get url from a page that has pagination?

I want to get url from 5 page at the same time so I write my code like this
<?php
$getLinks = "http://realestate.com.kh/real-estate-for-sale-in/all/";
for($i=1; $i<=5; $i++){
$result = $getLinks.$i;
$urls = file_get_contents($result);
$dom = new DOMDocument();
#$dom->loadHTML($urls);
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//div[contains(#class, 'featured') or contains(#class, 'premium')]//a");
for($i=0; $i<$hrefs->length; $i++) {
$href = $hrefs->item($i);
$url = $href->getAttribute('href').PHP_EOL;
echo $url."<br />";
}
}
?>
Here
$getLinks = "http://realestate.com.kh/real-estate-for-sale-in/all/";
for($i=1; $i<=5; $i++){
$result = $getLinks.$i;
will output
http://realestate.com.kh/real-estate-for-sale-in/all/1
http://realestate.com.kh/real-estate-for-sale-in/all/2
http://realestate.com.kh/real-estate-for-sale-in/all/3
http://realestate.com.kh/real-estate-for-sale-in/all/4
http://realestate.com.kh/real-estate-for-sale-in/all/5
each of this 5 url has different 20 url. I want to loop all of them to get all the url.
So if I loop 5 url above I will get 100 url. But in my code above doesn't work I can get only 20 url form http://realestate.com.kh/real-estate-for-sale-in/all/1.
Please help me everyone; Thanks.
your code seemed to be right except a tiny mistake which is as follows:
in both of your for Loop you are using the same variable $i as a loop iteration variable,
your second for loop, changes the value of $i and this value is used
by your first for loop.
I suggest you to change at least the second for loop iteration variable name. for e.g. replace $i with $j in your second for loop.

Facebook Pagination Limit with Graph API

this is my first post here.
I'm trying to query all places within my city limits (Winnipeg) using geo-location data and a radius of 15,000 meters.
This is working somewhat well with the query:
$request = new FacebookRequest(
$session,
'GET',
'/search?type=place&limit=100&center=49.89542,-97.13853&distance=15000'
);
The pagination afterwards works once or twice but ends up cutting me off after a variable 180-220 results. I noticed the pages returned are all from downtown (center query string) and start to spread outward, but it stops after only 2 city blocks.
Here is the loop I have in use for iterating through additional results:
$paging = $graphObject->asArray()['paging'];
$i = 0;
while(property_exists($paging, "next") && $i <= $times)
{
$next_url = $paging->next;
$json = file_get_contents($next_url);
$obj = json_decode($json);
foreach($obj->data as $page)
{
$page_ids[] = $page->id;
}
$paging = $obj->paging;
$i++;
}
I have researched issues with API limits but nothing that comes even close to only 200-ish results.
Is there possibly something I'm missing here?

PHP Array Shuffle HTML Links

Ok I'm going to try and explain this the best I can, I have 25 links in this format:
bla bla
First thing first, I need to add these 25 links into an array, which I am bit unsure of how to do it because its html, secondly I need to shuffle the array to choose 7 of them randomly and then display those 7.
Hope someone can help, this is beyond me, thanks in advance.
Ok, a little update, I have found a way of getting 1 html link to display randomly, could anyone help me with getting 7 out?
<?php
// Create the array
$links = array();
$links[0] = 'bla1';
$links[1] = 'bla2';
$links[2] = 'bla3';
// Count links
$num = count($links);
// Randomize order
$random = rand(0, $num-1);
// Print random link
echo $links[$random];
?>
For your second task :
Check array_rand() to retrieve X random values in your array.
http://www.php.net/manual/en/function.array-rand.php
If you care only about displaying these links randomized to the user then you can do with JavaScript like this http://jsfiddle.net/hVZL2/.
If you want to load these links into PHP array and do something with them after you still will have to use JavaScript. Convert the array that I created to JSON, send it via POST to some script that will parse JSON and you will have array of links.
As I can see you have your links on server.
<?php
// Create the array
$links = array();
$links[0] = 'bla1';
$links[1] = 'bla2';
$links[2] = 'bla3';
$links[3] = 'bla3';
$links[4] = 'bla3';
$links[5] = 'bla3';
$links[6] = 'bla3';
// Shuffle the array
shuffle($links);
// Display your links, note that we will display five links out of seven
for ($i = 0; $i < 5; $i++){
echo $links[$i];
}

passing php variables in query strings

I have a number of url's with different query strings such as
view.php?id=5
view.php?id=6
view.php?id=7
on another php page Im using file_get_contents as below:
$page = file_get_contents('view.php?id=5');
$file = 'temp/form.html';
file_put_contents($page, $file);
This of course only writes the first id '5', so how can i retrieve the 'id' variable on this page and write it in my file_get_contents line so I dont have to write out all the id's in seperate lines
thanks
rifki
If I understand correctly, in the situation you demonstrate you could use a for loop or something like that. But that only works if the IDs are numeric and follow each other up.
Example:
for($i = 5; $i <=7; $i++) {
$page = file_get_contents('view.php?id='.$i);
$file = 'temp/form.html';
file_put_contents($page, $file);
}
Updated:
If your ID comes from database you could select all IDs and loop through that.
Eg.
$sql = 'SELECT id FROM tablename;';
$res = mysql_query($sql);
while($row = mysql_fetch_assoc($res)) {
$page = file_get_contents('view.php?id='.$row['id']);
$file = 'temp/form.html';
file_put_contents($page, $file);
}
If those urls are used to browse some pages you can use the $_GET array (Official PHP Manual for the $_GET method).
It simply gets the value of a variable passed via the get method (i.e. page.php?var1=1&var2=2) so, if you need to get the id value for your page the code should be something like this:
$id = $_GET['id'];
$request = 'view.php?id='.$id;
$page = file_get_contents($request);
$file = 'temp/form.html';
file_put_contents($page, $file);
The first line gets the id passed via url, then the second creates the request string to pass to your file_get_contents function, then the other are like your code.
This is the case if you request the data from inside of such pages, if, for example, you know all of the pages needed then you can use a for clause to solve this problem.
One of the solutions might be:
$first_page = 5;
$last_page = 7;
for ($i = $first_page; $i <= $last_page; $i++) {
$request = 'view.php?id='.$i;
$page = file_get_contents($request);
$file = 'temp/form.html';
file_put_contents($page, $file);
}
With this you simply set the first and the last page you want to request, then you use these values to cycle through the pages and then call your function to do your... "stuff" :D
This is a good approach because then you can set in runtime the values for the for statement so you won't have to change that file every time.
However I think that using an identification different from Integers for your pages would be better, like id=home, or something like that.
If you get the id in your query string I mean url, you should write something like this:
$page = file_get_contents('view.php?id='.$_GET['id']);
$file = 'temp/form_'.$_GET['id'].'.html';
file_put_contents($page, $file);
To retrive the variable from a query string use
$_GET['variable_name']
By default, whenever you make a request to the server it is GET method which is called unless you explicitly specify that the form method to be POST.
//variable_name is the name of the variable in the query string

Categories