mysql - PHP 处理 XML 数据

标签 mysql php

我正在编写内容以从 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);

Warning: SimpleXMLElement::__construct(): http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in /.../indeedXMLSearch.php on line 44
Warning: 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): failed to open stream: no suitable wrapper could be found in /.../indeedXMLSearch.php on line 44 Warning: SimpleXMLElement::_construct(): I/O warning : failed to load external entity "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" in /.../indeedXMLSearch.php on line 44 Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /.../indeedXMLSearch.php:44 ...

最佳答案

在安全季节,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);
.....

关于mysql - PHP 处理 XML 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13660710/

相关文章:

php - 发布到 Facebook Graph Api 很慢

MySQL,如何重用分组中的计算数据

mysql - 如何将 select * 与 union 一起使用? mysql

mysql - MySQL 表名中的 UTF-8 增补字符?

mysql - SQL Match Against query for Multiple tables 的性能问题

php - 无法让登录页面重定向到安全页面

php - 将站点重定向到移动版本,谷歌移动友好测试出错

PHP插入大量值问题

php - 在PHP中导出为Excel和PDF,导致代码冗余

php - 如何在php中显示SUM()