I am attempting to parse wikipedia categories with the following URL:
…where “albert einstein” is any wikipedia page
The issue I’m having is that I’m not sure how to parse without hard coding the pageid, which is variable (in this case “736” – which only works for this page).
Note: I just started learning JSON today, so this may be an easy one.
$page = $_GET['page'];
$page = str_replace('_',' ',$page);
$url = 'http://en.wikipedia.org/w/api.php?
action=query&format=json&cllimit=max&prop=categories&titles='.$page;
echo $url;
$ch = curl_init($url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_USERAGENT, "TestScript");
$c = curl_exec($ch);
$json = json_decode($c);
$content = $json->{'query'}->{'pages'}->{'736'}->{'categories'};
print_r ($content);
One would assume there could, at least in theory, be multiple pages, so why not: