Forum Moderators: coopster
<?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>";
?>
$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
$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
[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);
$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
$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/________
)
)
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!