0

So i am trying to parse data from an XML url and insert it into a table using php, which can be seen here, (please keep in mind there are more products than displayed on this page, i am not trying to get it for just this Product,the code below shows how i am parsing all products) but i keep getting the following errors:


[EDITED]

class DataGrabber {

//The URL where data will be extracted from, which is an XML file
protected $URL = "http://json.zandparts.com/api/category/GetCategories/44/EUR/";

public function call_api($data) {


    if(count($data) == 0) return array();

    $jsondata = array();

    foreach($data as $entry){


        $url = $this->URL . $entry['model'] . "/" . urlencode($entry['family']) . "/" . urlencode($entry['cat']) . "/" . $entry['man'] . "/null";
        $json = file_get_contents($url);

        $data = json_decode($json, true);

        if(!empty($data['Products'])){
            foreach ($data['Products'] as $id => $product) {

                $jsonentry = array(
                    'productnumber' => $id,
                    'partnumber' => $product['strPartNumber'],
                    'description' => $product['strDescription'],
                    'manu' => $product['Brand']
                );

                $jsondata[] = $jsonentry;
            }
        }
    }

    return $jsondata;

}

}


[NEW ERRORS]

So i have fixed the error:

PHP Warning:  file_get_contents(http://json.zandparts.com/api/category/GetCategories/44/EUR/ET10B/E Series/AC Adapter/Asus/null): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request
 in /home/svn/dev.comp/Asus.php on line 82  

by using urlencode as shown in my code above

This warning below isnt finding the values for the url:

PHP Warning:  file_get_contents(http://json.zandparts.com/api/category/GetCategories/44/EUR///04G265003580/Asus/null): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found

as you can see after, 44/EUR there are three forward slashes with no data?? How would i resolve this??

ash
  • 93
  • 5
  • 14
  • 1
    Are you trying to process JSON with XML functions? – Álvaro González May 02 '13 at 09:06
  • @ÁlvaroG.Vicario: well it is confusing because it says it is an XML file but the url has json attached to it but yes the main thing is i am trying to parse all the data from the `url` here is one of the urls of the product: http://json.zandparts.com/api/category/GetCategories/44/EUR/ET1602/E%20Series/Battery/Asus/null – ash May 02 '13 at 09:17
  • If we open it in a browser in shows XML, but calling it in PHP the result is JSON. – Rolando Isidoro May 02 '13 at 09:20
  • Thanks. So how can i alter the above code to pars it with JSON instead? – ash May 02 '13 at 09:23
  • Maybe the PHP json functions? just a guess. – RockyFord May 02 '13 at 09:24
  • @RolandoIsidoro please see edit above.. – ash May 02 '13 at 13:10

2 Answers2

3

The remote server appears to use the Accept HTTP header to choose the output format. With PHP default options it sends back JSON instead of XML:

<?php
echo file_get_contents('http://json.zandparts.com/api/category/GetCategories/44/EUR/ET10B/E%20Series/AC%20Adapter/Asus/null');

... prints:

{"Categories":[{"Brand":null,"Fami...

To specify an Accept header you need to retrieve the data with some other function, e.g.:

<?php
$context = stream_context_create(
    array(
        'http' => array(
            'header' => "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n",
        )
    )
);
echo file_get_contents('http://json.zandparts.com/api/category/GetCategories/44/EUR/ET10B/E%20Series/AC%20Adapter/Asus/null', false, $context);

... prints:

<?xml version="1.0" encoding="utf-8"?><ProductCategory ...

Tweak it to your exact needs, I just copied the header from my browser.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
2

Here's a code snippet that shows you how to get the values for strPartNumber, strDescription and Brand for all the products in that JSON data:

<?php
    $url = 'http://json.zandparts.com/api/category/GetCategories/44/EUR/ET10B/E%20Series/AC%20Adapter/Asus/null';
    $json = file_get_contents($url);

    // Decode the JSON data as a PHP array
    $data = json_decode($json, true);

    if (!empty($data['Products'])) {
        foreach ($data['Products'] as $id => $product) {
            echo "Product #{$id}\n";
            echo "Part number: {$product['strPartNumber']}\n";
            echo "Description: {$product['strDescription']}\n";
            echo "Brand: {$product['Brand']}\n\n";
        }
    }

Output:

Product #0
Part number: 04G265003580
Description: POWER ADAPTER 65W19V 3PIN
Brand: Asus

Product #1
Part number: 14G110008340
Description: POWER CORD 3P L:80CM,TW(B)
Brand: Asus
Rolando Isidoro
  • 4,983
  • 2
  • 31
  • 43
  • So if i add these two lines as shown in my edit above, i'd be able to use the simplexml functions or would i need to take those out too? – ash May 02 '13 at 09:56
  • This solution doesn't use XML so you wouldn't be able to use SimpleXML functions. I prefer using JSON when possible, as it allows easier access to the data via PHP array as my code snippet shows. – Rolando Isidoro May 02 '13 at 10:14
  • I think i am getting somewhere, but i get the following errors displayed in my edit above.. – ash May 03 '13 at 10:59
  • There are 2 different problems, basically because of the URLs you're fetching. Work your way to understand what those [HTTP status codes](http://en.wikipedia.org/wiki/List_of_HTTP_status_codes) mean so you can overcome the problems. – Rolando Isidoro May 03 '13 at 13:01
  • Thanks. I have managed to solve the BadRequest but im still struck with the failure to open the stream as the URL i am using is correct? Please see my edit above.. – ash May 03 '13 at 13:59
  • The 3 slashes `///` occur when **`$entry['model']` and `urlencode($entry['family'])` are empty** in this line `$url = $this->URL . $entry['model'] . "/" . urlencode($entry['family']) . "/" . urlencode($entry['cat']) . "/" . $entry['man'] . "/null";`. – Rolando Isidoro May 03 '13 at 15:00
  • Yeh, i thought as much it would be that. Thanks for the heads up, i have it working now :-) – ash May 07 '13 at 09:29