【问题标题】:PHP Process XML DataPHP 处理 XML 数据
【发布时间】:2012-12-02 20:57:52
【问题描述】:

我正在写从 wbsite 读取 xml。

$jtype = $job_type == 1 ? 'fulltime' : $job_type == 2 ? 'parttime' : $job_type == 3 ? 'contract':'';
    $xml ="http://api.indeed.com/ads/apisearch?";
    $xml .="publisher=9628233567417007";
    $xml .="&q=".$q; //Query. By default terms are ANDed. To see what is possible, use our advanced search page to perform a search and then check the url for the q value.
    $xml .="&l=".$location; //Location. Use a postal code or a "city, state/province/region" combination.
    $xml .="&sort="; //Sort by relevance or date. Default is relevance.
    $xml .="&radius=30"; //Distance from search location ("as the crow flies"). Default is 25.
    $xml .="&st=employer"; //Site type. To show only jobs from job boards use 'jobsite'. For jobs from direct employer websites use 'employer'.
    $xml .="&jt=".$jtype ; //Job type. Allowed values: "fulltime", "parttime", "contract", "internship", "temporary".
    $xml .="&start=0"; //Start results at this result number, beginning with 0. Default is 0.
    $xml .="&limit=10"; //Maximum number of results returned per query. Default is 10
    $xml .="&fromage=".$within; //Number of days back to search.
    $xml .="&filter=1"; //Filter duplicate results. 0 turns off duplicate job filtering. Default is 1.
    $xml .="&latlong=1"; //If latlong=1, returns latitude and longitude information for each job result. Default is 0.
    $xml .="&co=GB";//arch within country specified. Default is us. See below for a complete list of supported countries. 
    $xml .="&v=2";

    $xmlData = new SimpleXMLElement( $xml, null, true);
    $xmls = $xmlData->xpath('results/result');

    $jIndeed = array();
    $iIndeed=1;
    if( !empty($xmls) )
    {
        foreach ( $xmls as $xml )
        {
            $created_at = strftime( dateFormat ,strtotime((string)$xml->date));
            $jIndeed[$iIndeed]['job_id']            = (string)$xml->jobkey;
            $jIndeed[$iIndeed]['jobTitle']          = cleanText( (string)$xml->jobtitle );
            $jIndeed[$iIndeed]['var_name']          = seoUrl( (string)$xml->jobtitle);
            $jIndeed[$iIndeed]['jobDescription']    = (string)$xml->snippet;
            $jIndeed[$iIndeed]['created_at']        = $created_at;
            $jIndeed[$iIndeed]['job_type']          = (string)$xml->typeName;
            $jIndeed[$iIndeed]['companyName']       = (string)$xml->company;
            $jIndeed[$iIndeed]['location']          = (string)$xml->formattedLocation;
            $iIndeed++;
        }
        $smarty->assign('searchIndeed', $jIndeed);
    }

当我在本地机器上运行它时,它工作正常,但是当我上传到我的网站时,我收到错误 500“页面无法显示”

我已将内存更改为 20MB,并将帖子更改为 1000,但仍然失败。我认为我的主机有限制,当我在 php 中设置仍然失败时,它没有任何区别,

有没有我可以用来处理这个网站xml的xml类。

更新:

放完这个ini_set('display_errors', E_ALL);

警告:SimpleXMLElement::__construct(): http:// 包装器在服务器配置中被 /.../indeedXMLSearch.php 第 44 行中的 allow_url_fopen=0 禁用
警告:SimpleXMLElement::_construct(http://api.indeed.com/ads/apisearch?publisher=9628233567417007&q=desktop&l=&sort=&radius=30&st=employer&jt=&start=0&limit=10&fromage=&filter=1&latlong=1&co =GB&v=2):无法打开流:在第 44 行的 /.../indeedXMLSearch.php 中找不到合适的包装器 警告:SimpleXMLElement::_construct(): I/O 警告:未能加载外部实体“http://api.indeed.com/ads/apisearch?publisher=9628233567417007&q=desktop&l=&sort=&radius=30&st=雇主&jt=&start=0&limit=10&fromage=&filter=1&latlong=1&co=GB&v=2" in /.../indeedXMLSearch.php 第 44 行 致命错误:/.../indeedXMLSearch.php:44 中带有消息“无法将字符串解析为 XML”的未捕获异常“异常”...

【问题讨论】:

  • 写入 ini_set('display_errors', E_ALL);在代码的第一行,然后运行它会显示所有错误。
  • 我添加了我现在遇到的错误
  • 您可以在此处找到有关您的错误的更多信息:php.net/manual/en/filesystem.configuration.php 您的主机禁用了将另一个 URL 简单地加载为本地文件的能力。您可以尝试查看是否在您的主机上启用了 curl
  • @koopajah 添加后 ini_set("auto_detect_line_endings", true);它是显示。这些安全吗?
  • 尝试使用我的答案中的解决方案。

标签: mysql php


【解决方案1】:

在安全季节,php 在默认设置中禁用 fopen url。最好根据php curl lib获取xml文件内容保存到本地文件。

然后使用new SimpleXMLElement ($localxml)

示例代码:

$xml = "http://....";

$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $xml);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

// grab URL and pass it to the browser
$xmlcontent = curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);

file_put_contents('/tmp/xmlfile', $xmlcontent);
$xmlelement = new SimpleXMLElement( $xml, null, true);
.....

【讨论】:

  • 我可以在不使用 fopen 的情况下编写自己的类来做到这一点
  • $xmlelement = new SimpleXMLElement($xml, null, true);仍在使用原始 xml 文件
【解决方案2】:

试试这个

$xmlData  = simple_xml_load_file(file_get_contents($xml));
print_r($xmlData);

而不是

 $xmlData = new SimpleXMLElement( $xml, null, true);

【讨论】:

  • 我在我的主机上使用 php 4。我通过 file_get_contents 仅在 php 5 中
  • 致命错误:在中调用未定义函数 simple_xml_load_file()
  • file_get_contents 也会失败,而 allow_url_fopen 为假。
  • 对不起我的错误。更正的功能我明白了。警告:simplexml_load_file() [function.simplexml-load-file]:I/O 警告:加载外部实体失败
猜你喜欢
  • 1970-01-01
  • 2012-07-09
  • 2011-03-10
  • 1970-01-01
  • 1970-01-01
  • 2017-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多