无法使用simplexml_load_file PHP读取XML

时间:2013-05-02 09:04:35

标签: php simplexml

所以我试图解析XML网址中的数据并使用php将其插入到表中,可以看到here,(请记住,此页面上显示的产品数量多于此页面,我是不试图只为这个产品,下面的代码显示我如何解析所有产品)但我不断收到以下错误:


[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;

}

}


[新错误]

所以我修复了错误:

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  

使用上面代码中显示的urlencode

以下警告未找到网址的值:

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

正如您所看到的,44/EUR有三个正斜杠,没有数据?我将如何解决这个问题?

2 个答案:

答案 0 :(得分:3)

远程服务器似乎使用Accept HTTP标头来选择输出格式。使用PHP默认选项,它会发送回JSON而不是XML:

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

...打印:

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

要指定Accept标头,您需要使用其他功能检索数据,例如:

<?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);

...打印:

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

根据您的确切需要调整它,我只是从浏览器中复制了标题。

答案 1 :(得分:2)

以下是一段代码片段,向您展示如何获取该JSON数据中所有产品的 strPartNumber strDescription 品牌的值:

<?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";
        }
    }

输出:

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