I am trying to get an array from the AnimeCharactersDatabase in order to then produce a table with the results. I had it working once before but cannot remember how I got it to work.
Looking at the url (http://www.animecharactersdatabase.com/api_series_characters.php?character_q=Usagi), "search_results" should be itself an array of characters which have arrays of info within them.
<?php
$url= "http://www.animecharactersdatabase.com/api_series_characters.php?character_q=Usagi";
/* gets the data from a URL */
function get_acdb($url)
{
//ACDB requires certain agents for the query per their documentation.
$agents = array(
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:7.0.1) Gecko/20100101 Firefox/7.0.1',
'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.9) Gecko/20100508 SeaMonkey/2.0.4',
'Mozilla/5.0 (Windows; U; MSIE 7.0; Windows NT 6.0; en-US)',
'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7; da-dk) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1' ,
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0 Waterfox/56.2.14',
'Lynx/2.8.7dev.4 libwww-FM/2.14 SSL-MM/1.4.1 OpenSSL/0.9.8d',
'Lynx/2.8.9dev.8 libwww-FM/2.14 SSL-MM/1.4.1 GNUTLS/3.4.9',
'Lynx/2.8.3dev.9 libwww-FM/2.14 SSL-MM/1.4.1 OpenSSL/0.9.6',
'Opera/9.80 (Windows NT 5.3; U; x64; en-US) Presto/2.12.388 Version/12.18'
);
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
//I believe this should make it return the data, not just true.
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
curl_setopt($ch,CURLOPT_USERAGENT,$agents[array_rand($agents)]);
$data = curl_exec($ch);
curl_close($ch);
return json_decode($data, true);
}
/* Parse the data into Characters. */
$arr = get_acdb($url);
$arr = array($arr["search_results"]);
//Upon testing, this is always an array of 1, and $arr[0] shows no value. This is where I need help, making the for each loop actually add characters to the new array SearchArray.
$i=-1;
foreach ($arr[0] as $xyz) {
//I never get within this function because there $arr[0] doesn't seem to be an array. $i=$i+1;
$CharName = $arr[0][$i]["name"];
$CharID = $arr[0][$i]["id"];
$SeriesID = $arr[0][$i]["anime_id"];
$SeriesName = $arr[0][$i]["anime_name"];
$medialenA = strlen($CharName) + 25;
$medialenB = strlen($SeriesName) + 2;
$mediatype = substr($arr[0][$i]["desc"],$medialenA);
$mediatype = substr($mediatype,0,strlen($mediatype) -$medialenB);
$CharSex = $arr[0][$i]["gender"];
{
//Add relevant matches to Array.
$SearchArray[] = array(
'CharID'=>$CharID,
'Name'=>$CharName,
'SeriesID'=>$SeriesID,
'SeriesName'=>$SeriesName,
'Sex'=>$CharSex,
);
}
}
?>
I have modified a bit your code but it is fully functional
<?php
$url = "https://www.animecharactersdatabase.com/api_series_characters.php?character_q=Usagi";
function getData($url){
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
//I believe this should make it return the data, not just true.
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:7.0.1) Gecko/20100101 Firefox/7.0.1');
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$results= getData($url);
$objectResults= json_decode($results);
foreach($objectResults->search_results as $character){
echo $character->anime_id."\n";
echo $character->anime_name."\n";
echo $character->anime_image."\n";
echo $character->character_image."\n";
echo $character->id."\n";
echo $character->gender."\n";
echo $character->name."\n";
echo $character->desc."\n";
echo"\n\n";
}