Tutorial :How can I fix this PHP XML parsing error?



Question:

This is my first question :).

Im writing a little twitter app in PHP that sends a DMs to all your followers. What im trying to do right now is to get the list of followers. So through twitter api and getting all usernames but for some reason this parsing error appear. Im new to php(but not so much to programming), I actually started learning it yesterday so please be easy on me ;).

Here is the code:

$t= new twitter();  $t->username= $_GET["username"];  $t->password= $_GET["password"];  $fi = $t->followers();      $xml[$page] = new SimpleXMLElement($fi[2]);      $user1count=0;      while(isset($xml[$page]->user[0])){            foreach ($xml[$page]->user as $user) {               $userdet[(string)$user->screen_name]=array( ’screen_name’=> (string)$user->screen_name, ‘location’=>(string)$user->location, ‘description’=>(string)$user-> description, ‘profile_image_url’=> (string)$user-> profile_image_url, ‘url’=>(string)$user-> url, ‘name’=>(string)$user->name );               $user1details[$user1count]= (string)$user->screen_name;               $user1count++;             }             $page++;            $fi=getfilecontents($friendsurl.$username1."xml?page".$page);            if($fi[0]===false){                 echo ("Error :".$fi[1]);                 $err=new SimpleXMLElement($fi[2]);                 echo " ".$err->error." ";                 // echo ““;                 die();            }         $xml[$page] = new SimpleXMLElement($fi[2]);      }  

And the error said:

Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /Applications/XAMPP/xamppfiles/htdocs/scripts/dmsend.php:125 Stack trace: #0 /Applications/XAMPP/xamppfiles/htdocs/scripts/dmsend.php(125): SimpleXMLElement->__construct('') #1 {main} thrown in /Applications/XAMPP/xamppfiles/htdocs/scripts/dmsend.php on line 125

Thank you! :)


Solution:1

It looks like $fi[2] is not a valid xml string. I am not 100% familiar with the twitter API, but I would do a var_dump($fi) and evaluate what is begin returned. From there, you should be able to figure out what is happening.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »