Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Feb 2004
    Location
    India
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    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";
    }
    ?>
    --------------------------------------------------
    Jagan Nath
    Web Development Company India SEO Services Since 1998 - Custom PHP Web Development

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    1. isset is missused here. An example:
    PHP Code:
    <?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/functio...t-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:
    PHP Code:
    $mydebug true;

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

    best regards
    Last edited by oesxyl; 08-23-2008 at 02:25 PM.

  • #3
    New Coder
    Join Date
    Feb 2004
    Location
    India
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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;

    ?>
    Jagan Nath
    Web Development Company India SEO Services Since 1998 - Custom PHP Web Development

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Jagan View Post
    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.

    Quote Originally Posted by Jagan View Post
    PHP Code:
    $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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •