Page 1 of 2 12 LastLast
Results 1 to 15 of 18

 

Thread: XML Feed into mysql

  1. #1
    apg1985's Avatar
    Registered User

    Status
    Offline
    Join Date
    Mar 2011
    Posts
    412
    Thanks
    1
    Thanked 17 Times in 11 Posts


    Ok,

    So ive been looking into this for a few hours now and im about to stick a folk in my eye, I cant for the life of me get this to work.

    In my hosting I have db_connect.inc with db connection details / I have a file containing the below script which is not working / I have a folder called xmlfeeds with an empty xml file called productfeed.xml

    When I run the script the xml file does not update, it stays how it is and the following error is thrown:

    Parse error: syntax error, unexpected '{' in myurl/script.php on line 12

    PHP Code:
    <?php
    require_once("db_connect.inc");
    db_connect();

    $xmlReader = new XMLReader();
    $filename "productfeed.xml";
    $url =  "http://datafeed.api.productserve.com/datafeed/download/apikey/08d521ce1e75d09d7e68a4d65d60cb4f/cid/281,283,554,285,555,286,282,287,288/columns/merchant_id,merchant_name,aw_product_id,merchant_product_id,product_name,description,category_id,category_name,merchant_category,aw_deep_link,aw_image_url,search_price,delivery_cost,merchant_deep_link,merchant_image_url,aw_thumb_url,brand_id,brand_name,commission_amount,commission_group,condition,currency,delivery_time,display_price,ean,in_stock,is_hotpick,isbn,is_for_sale,language,merchant_thumb_url,model_number,mpn,parent_product_id,pre_order,product_type,promotional_text,rrp_price,specifications,stock_quantity,store_price,upc,valid_from,valid_to,warranty,web_offer/format/xml/compression/zip/";

    file_put_contents($filenamefile_get_contents($url));

    $zip = new ZipArchive();
    if (
    $zip->open($filename) == TRUE {
    $zip->extractTo('xmlfeeds');
    $zip->close();
    }
    else {
    echo 
    "Extraction failed";
    }
    $xmlReader->open('xmlfeeds/productfeed.xml');

    while(
    $xmlReader->read())
        {

        switch (
    $xmlReader->name)
            {
            
            case 
    'product':
                
                
    $dom = new DOMDocument();
                
    $domNode $xmlReader->expand();
                
    $element $dom->appendChild($domNode);
                
    $domString utf8_encode($dom->saveXML($element));
                
    $product = new SimpleXMLElement($domString);
                
                
    $id $prod->prod['id'];
                
    $name $product->name;
                
    $tracking $product->awTrack;
                
                
                if(
    strlen($prod) > 0)
                {
                
                
    $query mysql_query ("REPLACE INTO ddtest (id, name, deeplink) VALUES ('$id', '$name', '$tracking')");
                echo 
    $name." has been instered!";
                
                }
            break;
        }

    ?>

  2. #2
    Registered User

    Status
    Online
    Join Date
    Jun 2006
    Posts
    628
    Thanks
    7
    Thanked 70 Times in 67 Posts
    not a PHP guy but this line
    if ($zip->open($filename) == TRUE {
    doesn't look right - maybe
    if ($zip->open($filename) == TRUE) {

  3. #3
    scriptmonkey's Avatar
    Oranges & Lemons

    Status
    Offline
    Join Date
    Jan 2009
    Location
    Worthing
    Posts
    1,607
    Thanks
    112
    Thanked 253 Times in 192 Posts
    What he said ^
    The trouble with the rat race is that even if you win you're still a rat.
    Time passes. Listen. Time passes. Dylan Thomas
    Ebay Alerts to your inbox

  4. #4
    apg1985's Avatar
    Registered User

    Status
    Offline
    Join Date
    Mar 2011
    Posts
    412
    Thanks
    1
    Thanked 17 Times in 11 Posts
    Ok so when I get a chance ill give it ago.

    Another question - say if the feed on a monday has 10 offers and they go into the database from my script.

    Now say on the tuesday the 10 offers from monday have expired and 10 NEW offers are in the feed.

    When the script adds these to the database will it overwrite the old ones or put them in under the old ones?

    Basically will I have 10 rows on the tuesday or 20.

  5. #5
    scriptmonkey's Avatar
    Oranges & Lemons

    Status
    Offline
    Join Date
    Jan 2009
    Location
    Worthing
    Posts
    1,607
    Thanks
    112
    Thanked 253 Times in 192 Posts
    Depends on how you set up your database, if you make the link url (or guid) the primary, no duplicates field, then you won't overwrite anything that already exists, and, you'll add the new stuff to the end of the table.
    The trouble with the rat race is that even if you win you're still a rat.
    Time passes. Listen. Time passes. Dylan Thomas
    Ebay Alerts to your inbox

  6. #6
    Registered User

    Status
    Online
    Join Date
    Jun 2006
    Posts
    628
    Thanks
    7
    Thanked 70 Times in 67 Posts
    Quote Originally Posted by apg1985 View Post
    Ok so when I get a chance ill give it ago.

    Another question - say if the feed on a monday has 10 offers and they go into the database from my script.

    Now say on the tuesday the 10 offers from monday have expired and 10 NEW offers are in the feed.

    When the script adds these to the database will it overwrite the old ones or put them in under the old ones?

    Basically will I have 10 rows on the tuesday or 20.
    Whichever you want :-)

    Why not just delete the existing offers then import the new ones?

  7. #7
    Registered User

    Status
    Offline
    Join Date
    Mar 2007
    Posts
    68
    Thanks
    0
    Thanked 4 Times in 4 Posts
    Just a Tip here , i would do this in real time as the price value will be out of date every day if you miss the update. plus if your insert fails to update successfully . No need to dump all the info

  8. #8
    nick-harper's Avatar
    Registered User

    Status
    Offline
    Join Date
    Feb 2011
    Posts
    91
    Thanks
    0
    Thanked 4 Times in 4 Posts
    Quote Originally Posted by webboy View Post
    Just a Tip here , i would do this in real time as the price value will be out of date every day if you miss the update. plus if your insert fails to update successfully . No need to dump all the info
    That would slow down page load time though, and with Google counting this it isn't worth it.

    It depends how many feeds you have and the size of it really. Just set it as a regular CRON for every few hours so you won't ever forget.

  9. #9
    Registered User

    Status
    Offline
    Join Date
    Mar 2011
    Posts
    59
    Thanks
    0
    Thanked 1 Time in 1 Post
    agree with the above post...

    your code at the moment just inserts because you haven't said WHERE to replace... You can either use "TRUNCATE TABLE mytable" to delete everything in there, then insert from the feed starting fresh

    or you could REPLACE INTO mytable WHERE id = $id

    which will do... well, what it says replace with the newer info where the id matches

  10. #10
    apg1985's Avatar
    Registered User

    Status
    Offline
    Join Date
    Mar 2011
    Posts
    412
    Thanks
    1
    Thanked 17 Times in 11 Posts
    Yea was going to do a cron job.

    Only thing which is happening with code above is it runs smoothly so no errors but the file name ive asked it to create puts it in the root and not the directory 'xmlfeeds' and the file which it does create on the root contains a load of rubbish where its trying to store the zip file itself in the xml file.

    But it also stores the correct file from the zip inside the directory but as the file name actually in the xml feed so something like 687598_xmlfile.xml.

    So basically when it runs I get a xml file in root called productfeed.xml with zipfile code in it and another in directory xmlfeeds which contains xml data from feed but not renamed.

    p.s Xerath ill get back to you soon been busy outside of affiliate stuff.

  11. #11
    Registered User

    Status
    Offline
    Join Date
    Mar 2011
    Posts
    59
    Thanks
    0
    Thanked 1 Time in 1 Post
    If you don't want it in the ROOT, the filename can actually contain the path as well.. eg/

    $filename = "/user/mysite.com/www/xmlfeeds/productfeed.xml";




    I'm not sure about the line $zip->extractTo('xmlfeeds');

    Maybe you need a full-path in there too?

  12. #12
    apg1985's Avatar
    Registered User

    Status
    Offline
    Join Date
    Mar 2011
    Posts
    412
    Thanks
    1
    Thanked 17 Times in 11 Posts
    Ok so I got the file going into the correct folder but now its not storing it in the db, goes through to end but gives no error??

    This is what I got so far:

    PHP Code:
    <?php
    require_once("db_connect.inc"); 
    db_connect();
    $xmlReader = new XMLReader();
    $filename "xmlfeeds/datafeed_112359.xml";
    $url =  "http://datafeed.api.productserve.com/datafeed/download/apikey/08d521ce1e75d09d7e68a4d65d60cb4f/cid/281,283,554,285,555,286,282,287,288/columns/merchant_id,merchant_name,aw_product_id,merchant_product_id,product_name,description,category_id,category_name,merchant_category,aw_deep_link,aw_image_url,search_price,delivery_cost,merchant_deep_link,merchant_image_url,aw_thumb_url,brand_id,brand_name,commission_amount,commission_group,condition,currency,delivery_time,display_price,ean,in_stock,is_hotpick,isbn,is_for_sale,language,merchant_thumb_url,model_number,mpn,parent_product_id,pre_order,product_type,promotional_text,rrp_price,specifications,stock_quantity,store_price,upc,valid_from,valid_to,warranty,web_offer/format/xml/compression/zip/";

    file_put_contents($filenamefile_get_contents($url));

    $zip = new ZipArchive();
    if (
    $zip->open($filename) == TRUE) {
    $zip->extractTo('xmlfeeds');
    $zip->close();
    }
    else {
    echo 
    "Extraction failed";
    }
    $xmlReader->open('xmlfeeds/datafeed_112359.xml');

    while(
    $xmlReader->read())
        {

        switch (
    $xmlReader->name)
            {
            
            case 
    'prod':
                
                
    $dom = new DOMDocument();
                
    $domNode $xmlReader->expand();
                
    $element $dom->appendChild($domNode);
                
    $domString utf8_encode($dom->saveXML($element));
                
    $product = new SimpleXMLElement($domString);
                
                
    $id $prod->prod['id'];
                
    $name $prod->name;
                
    $deeplink $prod->awTrack;
                
                
                if(
    strlen($id) > 0)
                {
                
                
    $query mysql_query ("REPLACE INTO test (id, name, deeplink) VALUES ('$id', '$name', '$deeplink')");
                echo 
    $id." has been instered!";
                
                }
            break;
        }

    ?>

  13. #13
    Registered User

    Status
    Offline
    Join Date
    May 2010
    Posts
    44
    Thanks
    0
    Thanked 7 Times in 7 Posts
    Try this,
    PHP Code:
    <?php
    require_once("db_connect.inc"); 
    db_connect();
    $filename  "xmlfeeds/datafeed_112359.xml";
    $url       "http://datafeed.api.productserve.com/datafeed/download/apikey/08d521ce1e75d09d7e68a4d65d60cb4f/cid/281,283,554,285,555,286,282,287,288/columns/merchant_id,merchant_name,aw_product_id,merchant_product_id,product_name,description,category_id,category_name,merchant_category,aw_deep_link,aw_image_url,search_price,delivery_cost,merchant_deep_link,merchant_image_url,aw_thumb_url,brand_id,brand_name,commission_amount,commission_group,condition,currency,delivery_time,display_price,ean,in_stock,is_hotpick,isbn,is_for_sale,language,merchant_thumb_url,model_number,mpn,parent_product_id,pre_order,product_type,promotional_text,rrp_price,specifications,stock_quantity,store_price,upc,valid_from,valid_to,warranty,web_offer/format/xml/compression/zip/";

    file_put_contents($filenamefile_get_contents($url));

    $zip = new ZipArchive();
    if (
    $zip->open($filename) == TRUE) {
        
    $zip->extractTo('xmlfeeds');
        
    $zip->close();
    } else {
        echo 
    "Extraction failed";
    }

    $xml simplexml_load_file($filename);

    foreach (
    $xml->merchant as $merchant) {
        foreach (
    $merchant->prod as $product) {
            
    $id   $product->attributes()->id;
            
    $name $product->text->name;
            
    $url  $product->uri->awTrack;
            
            
        if(
    strlen($id) > 0)
                {
                
                
    $query mysql_query("REPLACE INTO test (id, name, deeplink) VALUES ('$id', '$name', '$url')");
                echo 
    $id." has been inserted!</br>";
                
                }
        }
    }
    Last edited by michaeldim; 17-07-11 at 05:33 PM. Reason: Changed to PHP tags

  14. The Following User Says Thank You to michaeldim For This Useful Post:

    apg1985 (19-07-11)

  15. #14
    apg1985's Avatar
    Registered User

    Status
    Offline
    Join Date
    Mar 2011
    Posts
    412
    Thanks
    1
    Thanked 17 Times in 11 Posts
    Sweet, that worked perfectly.

    Thanks dude.

  16. #15
    apg1985's Avatar
    Registered User

    Status
    Offline
    Join Date
    Mar 2011
    Posts
    412
    Thanks
    1
    Thanked 17 Times in 11 Posts
    Uh problem, I was testing it with 1 merchant in the xml feed, if there are two or more the structure of the xml changes

    Check feed out with more then one merchant:

    http://datafeed.api.productserve.com...mpression/zip/

Page 1 of 2 12 LastLast


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Content Relevant URLs by vBSEO 3.5.0 RC2