0

I'm writing to read a xml from a wbsite.

$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);
    }

When I run this on local machine it works fine but when I upload to my site I get error 500 "Page cannot be display"

I have change the memery to 20MB and chnage the post to 1000 but still failing. I think my host has limit, it does not make any differece when i set in php is still failed,

Does any have xml class I can used to process this website xml.

UPDATE:

After Putting this 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 ...

6
  • 1
    write ini_set('display_errors', E_ALL); at first line of your code and then run it will show all errors. Commented Dec 1, 2012 at 15:12
  • I have added the error i'm geeting now Commented Dec 1, 2012 at 15:18
  • You can find more info about your error here : php.net/manual/en/filesystem.configuration.php Your host disabled the ability to simply load another URL as a local file. You could try to see if curl is enabled on your host Commented Dec 1, 2012 at 15:18
  • @koopajah after adding ini_set("auto_detect_line_endings", true); it is display. are these safe do so. Commented Dec 1, 2012 at 15:26
  • Try using my solution from answers. Commented Dec 1, 2012 at 15:27

2 Answers 2

1

For security season, php disable fopen url in default setting. You 'd better get xml file content according php curl lib and save it to local file.

Then use new SimpleXMLElement ($localxml).

Example Code:

$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);
.....
Sign up to request clarification or add additional context in comments.

2 Comments

Can I write my own class to do this without using fopen
$xmlelement = new SimpleXMLElement( $xml, null, true); is still using the orginal xml file
0

Try this

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

instead of

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

4 Comments

I'm using php 4 on my host. i through file_get_contents is only in php 5
Fatal error: Call to undefined function simple_xml_load_file() in
file_get_contents will also be fail while allow_url_fopen is false.
Sorry my mistake. corrected function i get this. Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.