0

因此,我正在尝试从 XML url 解析数据并使用 php 将其插入到表中,可以在此处看到,(请记住,此页面上显示的产品多于显示的产品,我不只是为了获取它此产品,下面的代码显示了我如何解析所有产品)但我不断收到以下错误:


[编辑]

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如我上面的代码所示

下面的这个警告没有找到 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

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

4

2 回答 2

3

远程服务器似乎使用AcceptHTTP 标头来选择输出格式。使用 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 ...

根据您的确切需求对其进行调整,我只是从浏览器中复制了标题。

于 2013-05-02T09:26:38.907 回答
2

下面的代码片段向您展示了如何获取该JSON 数据中所有产品的strPartNumberstrDescriptionBrand的值:

<?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
于 2013-05-02T09:24:57.423 回答