Forum Moderators: coopster

Message Too Old, No Replies

empty respsonse from curl

         

mattch80

5:15 pm on Jun 17, 2009 (gmt 0)

10+ Year Member



I've been trying to use curl with jquery to embed stuff on a webpage. Working back from a tutorial, I was able to get it working with a couple of feeds. But I can't get it working with this feed. The response I get back is just <results></results>. Any suggestions? Is there a way to see what's going wrong?

<?php

$request = "http://news.cnet.com/2547-1_3-0-20.xml";

$curl= curl_init();

curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);

curl_setopt ($curl, CURLOPT_URL,$request);

$response = curl_exec ($curl);

curl_close($curl);

$xml = simplexml_load_string($response);

echo "<results>";

for($i=0;$i<count($xml->item);$i++)
{
$title = $xml->item[$i]->title;

$link = $xml->item[$i]->link;

echo "<div class='result'><a href='" . $link . "'>" . $title . "</a></div>";
}

echo "</results>";

?>

jatar_k

5:23 pm on Jun 17, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I assume when you put the url in the browser it returns something

have you just echo'ed what's in $result? is that empty?

have you tried adding a user agent? maybe they don't like bots

mattch80

5:34 pm on Jun 17, 2009 (gmt 0)

10+ Year Member



I get back just <results></results>, so everything in my loop is being ignored. I tried adding a useragent, but it didn't help.

jatar_k

5:52 pm on Jun 17, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I mean just echo $response after you call curl exec

$response = curl_exec ($curl);
echo $response;
die;

to see if you are getting no result from curl_exec or something else is happening with simplexml_load_string or your loop. You could also check for errors and get the http code like so

$response = curl_exec ($curl);
$curlerror = curl_error($curl);
$httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
echo '<p>response: ',$response;
echo '<p>error: ',$curlerror;
echo '<p>http code: ',$httpcode;
die;

just to help isolate the problem

you also didn't answer whether it works in a browser

mattch80

6:02 pm on Jun 17, 2009 (gmt 0)

10+ Year Member



The browser shows a blank page, but the source code shows <results></results>.

When I echo out $response I get the original feed displayed in the browser.

I didn't get any errors. For the http code I got back 200.

jatar_k

6:09 pm on Jun 17, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



that's good news

the next step would be to remove the previous error checking and do

$xml = simplexml_load_string($response);
echo $xml;
die;

to see if that function is working

mattch80

1:28 am on Jun 18, 2009 (gmt 0)

10+ Year Member



I tried that (echo $xml; die;) and got a blank page. What should it look like?

[edited by: encyclo at 5:53 pm (utc) on June 18, 2009]
[edit reason] disabled unintentional smiley [/edit]

jatar_k

12:25 pm on Jun 18, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



actually I was mistaken, forgot it loads it in as an object, try

$xml = simplexml_load_string($response);
echo '<pre>';
print_r($xml);
echo '</pre>';
die;

which should output the object which contains the page you grabbed, if it got properly loaded into the object, then you need to play with your loop because that would be where the problem is

if it doesn't output the object then that function is what's not working

mattch80

1:35 pm on Jun 18, 2009 (gmt 0)

10+ Year Member



Yes, it did output the object. Here's part of it:


[item] => Array
(
[0] => SimpleXMLElement Object
(
[title] => stuff
[link] => stuff
[description] => stuff
[category] => SimpleXMLElement Object
(
)
[guid] => stuff
[pubDate] => stuff
)

I tried echoing the counter in my loop, and didn't get any results.


for($i=0;$i<count($xml->item);$i++) { echo $i; }

When I echo out just the inner part I'm getting 0.


echo count($xml->item);

jatar_k

2:58 pm on Jun 18, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



maybe just count is no good, try seeing what you get there, counts shouldn't be done in the loop anyway because then they need to be evaluated each time, but they never change

$howmany = count($xml->item);
echo $howmany;
die;

to see if the count is working, then you can have your for look like this after so the count is only done once

for($i=0;$i<$howmany;$i++) {

I am guessing the count isn't working which is why you get no content, if that echo's 0 then play with finding a way to make it count properly. You could also switch the test all together

maybe isset($xml->item[$i]) might work, not sure

mattch80

3:39 pm on Jun 18, 2009 (gmt 0)

10+ Year Member



I tried your suggestions, but no luck. I'm pretty sure it's my selection that isn't working. I'm not even getting a response if I do this:

echo $xml->item[1]->title;

mattch80

3:52 pm on Jun 18, 2009 (gmt 0)

10+ Year Member



OK, found the problem (should have been obvious). It's supposed to be:

$xml->channel->item[1]->title;

Thanks for showing me how to troubleshoot this.

jatar_k

4:10 pm on Jun 18, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



no problem, nice catch

mattch80

4:28 pm on Jun 18, 2009 (gmt 0)

10+ Year Member



Maybe this should be in a new thread, but how do you deal with the @ sign? It's producing a parse error.

$xml->channel->item[1]->@attributes->href;

jatar_k

5:37 pm on Jun 18, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



if it has to be there maybe there is some way of escaping it

but it is a special character used for error suppresion so that isn't proper usage

maybe there is another way to do what you need, what is the @ used for?

mattch80

6:09 pm on Jun 18, 2009 (gmt 0)

10+ Year Member



It's in the object I'm looking at. To get the second href value I'm going:

$xml->entry[$i]->link[1]->@attributes->href;


[entry] => Array
(
[0] => SimpleXMLElement Object
(
[id] => tag:search.twitter.com,2005:2225612870
[published] => 2009-06-18T17:56:17Z
[link] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[type] => text/html
[rel] => alternate
[href] => http://twitter.com/________/statuses/2225612870
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[type] => image/png
[rel] => image
[href] => http://________.jpg
)
)
)
[title] => ________
[content] => ________
[updated] => 2009-06-18T17:56:17Z
[author] => SimpleXMLElement Object
(
[name] => ________
[uri] => http://twitter.com/________
)
)

Pico_Train

5:48 am on Jun 19, 2009 (gmt 0)

10+ Year Member



I had something similar where I couldn't get anything and my response also had an @attributes in there.

I found this, $url being where you post to and $data being your xml string with the parameters you need:

function do_post_request($url, $data, $optional_headers = null)
{
$params = array('http' => array(
'method' => 'POST',
'content' => $data
));
if ($optional_headers !== null) {
$params['http']['header'] = $optional_headers;
}
$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;
}

Works fine for me. Hope that helps, nto even sure I understand what the problem is but...

I also use a function I found to spit out the xml in a "manageable" PHP array. If you want that too, let me know, will go dig it out!