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:
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?
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
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):