沃梦达 / 编程问答 / php问题 / 正文

不能用简单的exml_Load_FilePHP读取XML

Can#39;t read XML with simplexml_load_file PHP(不能用简单的exml_Load_FilePHP读取XML)

本文介绍了不能用简单的exml_Load_FilePHP读取XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我尝试从一个XML url中解析数据,并使用php将其插入到一个表中,here可以看到here(请记住,这里显示的产品比本页上显示的产品多,我并不是只为这个产品获取它,下面的代码显示了我是如何解析所有产品的),但我一直收到以下错误:


[编辑]

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有三个没有数据的正斜杠??我将如何解决此问题??

推荐答案

这里有一段代码片段,它向您展示了如何获取该json数据中所有产品的strPartNumberstrDescription品牌的值:

<?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}
";
            echo "Part number: {$product['strPartNumber']}
";
            echo "Description: {$product['strDescription']}
";
            echo "Brand: {$product['Brand']}

";
        }
    }

输出:

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

这篇关于不能用简单的exml_Load_FilePHP读取XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:不能用简单的exml_Load_FilePHP读取XML

基础教程推荐