从URL

时间:2015-08-10 15:08:54

标签: php xml

我搜索了网站并找到了许多资源来帮助我加载和解析XML文件,但是在按照一些示例后,我仍然无法使用它。我错过了什么?

感谢

<?php
$url = 'http://api.l5srv.net/job_search/api/web/find_jobs.srv?CID=2239&SID=u9xcvY234AA09&format=XML&q=Sales&l=95054&r=25&s=relevance&a=2014-09-30&start=1&limit=8&highlight=off&userip=25.158.22.121&useragent=Mozilla%2F5.0';

$xml = simplexml_load_file($url) or die("feed not loading");

var_dump($xml);
?>

2 个答案:

答案 0 :(得分:2)

您很可能会将您在该网址下的浏览器中看到的内容与XML文档混淆。

您在网址上的内容

  

http://api.l5srv.net/job_search/api/web/find_jobs.srv?CID=2239&SID=u9xcvY234AA09&format=XML&q=Sales&l=95054&r=25&s=relevance&a=2014-09-30&start=1&limit=8&highlight=off&userip=25.158.22.121&useragent=Mozilla%2F5.0

不是XML文档。当您请求该URL并查看响应标头时,您可以看到它是一个HTML文档:

HTTP/1.1 200 OK
Content-Type: text/html;charset=ISO-8859-1
P3P: CP="IDC CON TEL CUR DEV SAM IND"
Date: Mon, 10 Aug 2015 15:50:46 GMT
Content-Language: en-US
Connection: Keep-Alive
Set-Cookie: X-Mapping-gjinjpae=F462690912B62A0C5476B15FCDB01A81; path=/
Set-Cookie: JSESSIONID=BC6666D2A357FB969A60E05E67B0888C; Path=/
Set-Cookie: JSESSIONID=700D01B2F92909260A05215F86AB8EE5; Path=/
Content-Length: 7147

您还可以使用浏览器中的查看源功能轻松验证浏览器,或者看到XML未显示&#34;漂亮&#34;。< / p>

但是simplexml_load_file需要格式良好的XML文档。在您的情况下,您遇到的主要问题是缺少错误处理。当您与远程系统交互时,错误处理对于稳定使用至关重要,因此请将其作为脚本的一部分:

除此之外,由于它是HTML文档而不是XML文档,因此您需要使用HTML解析器解析它 - 而不是XML解析器。因此,不要在该阶段尝试使用XML解析器,首先使用HTML解析器。

<强> 编辑:

该服务的问题仅在您请求XML格式时。如果您将format参数更改为JSON(&format=JSON),您可以直接解析数据,尽管给出了错误的响应内容类型:

$url = 'http://api.l5srv.net/job_search/api/web/find_jobs.srv?CID=2239&SID=u9xcvY234AA09&format=JSON&q=Sales&l=95054&r=25&s=relevance&a=2014-09-30&start=1&limit=8&highlight=off&userip=25.158.22.121&useragent=Mozilla%2F5.0';

$result = json_decode(file_get_contents($url));

print_r($result);

给出:

Array
(
    [0] => stdClass Object
        (
            [response] => stdClass Object
                (
                    [query] => Sales
                    [location] => 95054
                    [highlight] => off
                    [totalresults] => 25406
                    [start] => 1
                    [end] => 9
                    [radius] => 25
                    [pageNumber] => 0
                    [results] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [jobtitle] => Sales
                                    [zip] => 95101
                                    [company] => Commercial Janitorial Company
                                    [city] => San Jose
                                    [state] => CA
                                    [country] => US
                                    [date] => 2015-07-14
                                    [url] => http://api.l5srv.net/job_search/api/web/get_job.srv?token=3aeyzv6V2SA%2BKYF5lzqWmxyivVwoE3LFernO291sVVpLbWCG9bBAbVO%2BCGXuN1V%2F9QMmDY3KeK5iYg2phrtjypXtQ82Jngf1q8zQIzix14EuBlSL96sqjffsuHozTZ4SJ6Mf%2B%2BVwRrC65gRtKxH6wg0F50WEZtnD9Xv0%2Bxc2GMhFMszKNEOyrfCNg5YTn%2Flj
                                    [snippet] => Company Description:

We are a Christian owned janitorial company doing business here in the Bay Area for nearly 40 years. You do not have to be Christian to work for us.
We operate in a fast paced, f
                                    [onmousedown] => l5_trk(this)
                                )

                            [1] => stdClass Object
                                (
                                    [jobtitle] => Sales
[...]

答案 1 :(得分:1)

来自此服务器的代码不是有效的XML。试试这个:

<?php
    $url = 'http://api.l5srv.net/job_search/api/web/find_jobs.srv?CID=2239&SID=u9xcvY234AA09&format=XML&q=Sales&l=95054&r=25&s=relevance&a=2014-09-30&start=1&limit=8&highlight=off&userip=25.158.22.121&useragent=Mozilla%2F5.0';

    $data = file_get_contents($url);
    $data = '<' . '?xml version="1.0" encoding="UTF-8"?' . '>' . str_replace(array("&lt;", "&gt;"), array("<", ">"), $data);

    $xml = simplexml_load_string($data) or die("feed not loading");

    var_dump($xml);