Twitter API - error in reading xml output

Discussion in 'PHP' started by krishmk, Jul 5, 2009.

  1. #1
    What is wrong on this code?
    I am creating a request to Twitter API thru post method (using cURL) for following the specified user (thru $screen_name).

    The follow request executes well as expected.
    However as you may be aware Twitter returns the success or error code in xml/ json format. In my code I am requesting xml format.

    When I run the following script, I see an xml output (the user details of whom we requested to follow - in xml format)

    But it ouputs the following error:
    Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 1: parser error : Start tag expected, '<' not found

    All I want is to collect the xml result in variables and output them in HTML format.

    $user and $pass are initialized well before this code. The below is the part of my script and the actual problem lies in the line where the function "simplexml_load_string()" is called.



    $postfields = "screen_name=$screen_name";
    //cURL initialization
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/cookie");
    curl_setopt($ch, CURLOPT_URL, "http://twitter.com/friendships/create/id.xml");
    curl_setopt($ch, CURLOPT_USERPWD, $user . ':' . $pass);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
    $data = curl_exec($ch);
    $http_response = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    
    if ($http_response == 200)
    {
    $xml2 = simplexml_load_string($data);
    for($i=0;$i<count($xml2->user);$i++)
    {
    echo ("You are now following: $screen_name");
    $id = $xml2->user[$i]->id;
    $name = $xml2->user[$i]->name;
    $imgurl = $xml2->user[$i]->profile_image_url;
    echo ("$imgurl<br />");
    echo ("name<br />");
    echo ("$id<br />");
    }
    }
    PHP:

     
    krishmk, Jul 5, 2009 IP
  2. krishmk

    krishmk Well-Known Member

    Messages:
    1,376
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    185
    #2
    Ok I was trying to fix this error and just came to know that "$data" returns either 0 or 1. So it tells us whether the connection failed or succeeded and thats the reason I got the xml error.

    Does anyone know as to how I can capture the response string (xml format) after friendship is created?
     
    krishmk, Jul 5, 2009 IP
  3. hassanahmad2

    hassanahmad2 Active Member

    Messages:
    243
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    60
    #3
    Are you trying to use a variable here?
    
    curl_setopt($ch, CURLOPT_URL, "http://twitter.com/friendships/create/id.xml");
    
    Code (markup):
    id should be $id
     
    hassanahmad2, Jul 6, 2009 IP
  4. hassanahmad2

    hassanahmad2 Active Member

    Messages:
    243
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    60
    #4
    Try something like this, it may work:

    
    function do_post_request($url)
    {
    	$params = array('http' => array(
    	'method' => 'POST',
    	'content' => null));
     
    	$ctx = stream_context_create($params);
    	$fp = @fopen($url, 'rb', false, $ctx);
    	
    	if (!$fp)
    	throw new Exception("Problem with $url, $php_errormsg");
    	
    	$response = @stream_get_contents($fp);
    	if ($response === false)
    		throw new Exception("Problem reading data from $url, $php_errormsg");
     
    	return $response;
    }
    
    $output = do_post_request("http://$username:$password@twitter.com/friendships/create/$usertofollow.xml?follow=true");
    $xml2 = simplexml_load_string($output);
    
    for($i=0;$i<count($xml2->user);$i++) {
      echo ("You are now following: $screen_name");
      $id = $xml2->user[$i]->id;
      $name = $xml2->user[$i]->name;
      $imgurl = $xml2->user[$i]->profile_image_url;
      echo ("$imgurl<br />");
      echo ("name<br />");
      echo ("$id<br />");
    }
    
    
    
    Code (markup):
     
    hassanahmad2, Jul 6, 2009 IP