Question

I am using WAMP server and I trying to read in a remote web service. I am receiving the following error -

Warning: simplexml_load_file(http://example.com/search-api/search/devapi/coupons?format=xml&key=xxxxxxxx&searchloc=30043): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in C:\wamp\www\php\ws.php on line 7

Below is my code.

if( ! $xml = simplexml_load_file('http://example.com/search-api/search/devapi/coupons?format=xml&key=xxxxxxxx&searchloc=30043') ) 
   { 
       echo 'unable to load XML file'; 
   } 
   else 
   { 
       echo 'xml loaded successfully';
   }
Was it helpful?

Solution

Solution 1

You can use file_get_contents

Try this -

print_r( simplexml_load_string( file_get_contents("http://pubapi.atti.com/search-api/search/devapi/search?searchloc=78597&term=pizza&format=xml&sort=distance&radius=5&listingcount=10&key=gmj3x7mhsh%22") ) );

Solution 2

function get_data($url){

    $ch = curl_init($url);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,  CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)');

    $data = curl_exec($ch);

    curl_close($ch);

    return $data;
}

$url = 'http://pubapi.atti.com/search-api/search/devapi/search?searchloc=78597&term=pizza&format=xml&sort=distance&radius=5&listingcount=10&key=gmj3x7mhsh%22';

$result = get_data($url);

print_r(simplexml_load_string($result));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top