...

View Full Version : XML Feed - Fatal error: Call to a member function children() on a non-object



Jagan
08-23-2008, 11:14 AM
Hi,

I am getting this error:

Fatal error: Call to a member function children() on a non-object in
/home/user/public_html/mydomain/result.php on line 17

I am using server with PHP 5.2 and Curl

------------------------------------------------------
<?php

/*
* REQUIRES PHP 5.1 AND CURL
*/

// step 1: get XML data from feed
include 'http://www.mydomain.info/contextual_data.php';
$my_min_cpc = 0.00;

// step 2: parse the feed
$xml = simplexml_load_string($xmlstr, 'SimpleXMLElement', LIBXML_NOCDATA);

if (isset($xml)) {
// step 3: extract the results

$children = $xml->children("urn:syndicatedResults");

if (isset($children)) {
// step 4: loop over each ad
$no_results = false;
foreach ($children->ssResultSet as $resultSet) {
foreach ($resultSet->ssResult as $result){
if (floatval($result->cpc) >= $my_min_cpc) {
echo "<div class='text_ad'>";
echo "<strong>" . $result->title . "</strong><br />\n";
echo $result->abstract . "<br />\n";
echo "<a href='" . $result->url . "'>";
echo $result->displayUrl;
echo "</a>\n</div>\n\n";
} else {
$no_results = true;
}
}
}


if ($no_results) {
echo "<div class='no_results'>No results were found for your search.</div>\n";
}
} else {
echo "<div class='no_matches'>No results were found for your search.</div>\n";
}
} else {
echo "<div class='error'>There was an error processing your search.</div>\n";
}
?>
--------------------------------------------------

oesxyl
08-23-2008, 02:22 PM
1. isset is missused here. An example:


<?php
$myvar = false;
if(isset($myvar)){
print "is false but is not unset or NULL\n";
}
$myvar = NULL;
if(!isset($myvar)){
print "is NULL or unset\n";
}
?>

documentation is unclear to what must return children():
http://www.php.net/manual/en/function.simplexml-element-children.php
but is clear that can't be tested with isset.
http://www.php.net/manual/en/function.isset.php

2. missing declaration of urn? not sure how it work because I don't use simplexml,
but I think you must declare namespace for urn:syndicatedResults somewhere if
is not declared in xml you get with curl( that's a guess)

3. if you use if to test returned values, this is not to popular but good
idea :), add a else and display everything you can get from that error, it help a lot, :)
You can add a variable as flag if you want to don't display that for public/users and make it true just for you. something like:


$mydebug = true;

.....
if($mydebug){
// show all you get here
}


best regards

Jagan
08-25-2008, 05:26 AM
Hi oesxyl!

Thanks for the reply....
I have no success!

XM File is like...



<?php
/*
* REQUIRES PHP 5.1 AND CURL
*/

// sample data for testing
$xmlstr = <<<XML
<?xml version="1.0" encoding="UTF-8" ?>
<sr:syndicatedResults xmlns:sr="urn:syndicatedResults">

<sr:ssResultSet sr:firstResult="1" sr:numResults="4" sr:totalHits="4">
<sr:ssResult sr:id="1">
<sr:abstract><![CDATA[Get a free direct quote now! Named #1 insurance website.]]></sr:abstract>
<sr:title><![CDATA[Great Car Insurance]]></sr:title>
<sr:url><![CDATA[http://track.domain.com/og/pr=Psr;ro=1;rc=4;digest=ba2bf9e1816b951be215ff8e9bb1e9dd;kid=128246deeb8f9b8520617ed167430b71;t=1189 791067;v=3;data=2ffa8c6c78211b3007b6e979adbb73c2ca1f1a7ad2f3a024d89ba30ec2da82fb;la=257550;lm=790041 ;kw=249264096;ed=;qt=car%20insurance;vr=;lt=EM;mt=E0;ip=;pt=2;st=37.33.3.2;os=2.0.0.0.0.0.0.0.15.0.0 .0.0.159.95.10.3;sy=keyword;my=smart;]]></sr:url>
<sr:displayUrl><![CDATA[insurance.example.com]]></sr:displayUrl>
<sr:cpc>0.01</sr:cpc>
</sr:ssResult>
</sr:ssResultSet>

</sr:syndicatedResults>
XML;

// replace with your feed code
$feed="1111";

// replace with URL of page where ad is displayed
$referrer="http://www.domain.info/result.php";

// replace with the end user's IP address; required
$ip="20.0.2.3";

// replace with the end user's User-Agent; required
$ua="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.8) Gecko/20071008 Firefox/2.0.0.8";

// replace with text from page content or keyword list
$hint="conference calling, mesothelioma , secured loans, insurance auto, austin
texas dwi lawyer, purchase structured settlements, data recovery denver,
endowments selling, irs tax lawyers, donate your car, eloan mortgage,
consolidate school loans";

// replace with URL of page where ad is displayed
$url="http://www.domain.info/result.php";

// Uncomment this line if you're using direct content matching
// $url=$hint;

// construct query
$query_url="http://adsprovider.com/r_search?isp=" . urlencode($feed) . "&rf=" . urlencode($referrer) . "&ip=" . urlencode($ip) . "&ua=" . urlencode($ua) . "&th=" . urlencode($hint) . "&q=" . urlencode($url);

// fetch results
$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
curl_setopt($ch, CURLOPT_URL,$query_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
$curl_info = curl_getinfo($ch);
curl_close($ch);

// replace test data with live data
// uncomment this line to perform a live query
$xmlstr = $result;

?>

oesxyl
08-25-2008, 10:59 PM
Hi oesxyl!

Thanks for the reply....
I have no success!

XM File is like...

the xml file is valid xml but not rss, try to validate it if this is rss.
This don't matter for your script it must work as is.

the problem is how you use simplexml.
try to use sr:syndicatedResults instead of urn:syndicatedResults and use var_dump for debug.





$children = $xml->children("urn:syndicatedResults");

if (isset($children)) {
// step 4: loop over each ad
$no_results = false;
foreach ($children->ssResultSet as $resultSet) {
foreach ($resultSet->ssResult as $result){
if (floatval($result->cpc) >= $my_min_cpc) {
echo "<div class='text_ad'>";
echo "<strong>" . $result->title . "</strong><br />\n";
echo $result->abstract . "<br />\n";
echo "<a href='" . $result->url . "'>";
echo $result->displayUrl;
echo "</a>\n</div>\n\n";
} else {
$no_results = true;
}
}
}


if ($no_results) {
echo "<div class='no_results'>No results were found for your search.</div>\n";
}
} else {
echo "<div class='no_matches'>No results were found for your search.</div>\n";
}
} else {
echo "<div class='error'>There was an error processing your search.</div>\n";
}
?>
--------------------------------------------------



PS: please put code between [ php] and [ /php] or [ code] and [ /code] tags.

best regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum