NBG Posted December 13, 2016 Share Posted December 13, 2016 Been out of the "loop" for a while and running into trouble on day one. Trying to figure it out. Parsing Data from a url and getting error message "Class '98007214' not found in" ------------------------------------- code as follows $corpid=array('98007214','98008630'); $x = 1; while ($x<3) { $url="https://api.eveonline.com/corp/CorporationSheet.xml.aspx?corporationID=" .$corpid($x); $data = file_get_contents($url); $list = new SimpleXMLElement($data); foreach ($list->result->corporationName as $result): echo $result; endforeach; $x=$x+1; } -------------------------- So it seems like the $corpid($x) expression is pulling the value it is supposed to be pulling. but then the error is because its looking for a class? Never had this issue back in the day and a bit confused on why its just not adding the varible in as text and creating a class error. Quote Link to comment Share on other sites More sharing options...
codefossa Posted December 13, 2016 Share Posted December 13, 2016 To pull the items of an array and append them to a string you would use: "your url ..." . $corpid[$x] Also note that you should probably check the array key exists, as you're doing 1 and 2 in your loop as $x when your array keys are 0 and 1. Try something more like .. // Number of elements in the array. $corpid_len = count($corpid); // Loop through starting with 0 and ending with the count - 1 for ($i = 0; $i < $corpid_len; $i++) { $url="https://api.eveonline.com/corp/CorporationSheet.xml.aspx?corporationID=" . $corpid[$x]; } Quote Link to comment Share on other sites More sharing options...
requinix Posted December 13, 2016 Share Posted December 13, 2016 o7 There's also the simpler foreach loop: foreach ($corpid as $id) { $url="https://api.eveonline.com/corp/CorporationSheet.xml.aspx?corporationID=" . $id;And SimpleXML supports loading directly from a URL so you can skip the file_get_contents() step and do $list = new SimpleXMLElement($url, 0, true); // true=first argument is a url Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted December 13, 2016 Share Posted December 13, 2016 On a side note: It's generally a good idea to assemble URL query strings with http_build_query(). This ensures all data is properly encoded, it's easier to add new parameters, and it's more readable (at least in my opinion). $url = 'https://api.eveonline.com/corp/CorporationSheet.xml.aspx?'.http_build_query(['corporationID' => $id]); Quote Link to comment Share on other sites More sharing options...
bsmither Posted December 15, 2016 Share Posted December 15, 2016 I think one misunderstanding is here: $corpid=array('98007214','98008630'); $x = 1; while ($x<3) Standard arrays are zero-based. That is, a var_dump($corpid) will show that $corpid has indexes of zero and one. Also, when assigning $url, the array $corpid is being used as a function call. So: From: $url="https://api.eveonlin...?corporationID=" .$corpid($x); // Not parentheses To: $url="https://api.eveonlin...?corporationID=" .$corpid[$x]; // Use brackets Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.