PHP。无法使用WAMP服务器使用远程Web服务

时间:2014-04-23 03:16:52

标签: php wamp

我正在使用WAMP服务器,我试图读取远程Web服务。我收到以下错误 -

  

警告:   使用simplexml_load_file(http://example.com/search-api/search/devapi/coupons?format=xml&key=xxxxxxxx&searchloc=30043):   无法打开流:HTTP请求失败! HTTP / 1.1 400错误请求   在第7行的C:\ wamp \ www \ php \ ws.php

以下是我的代码。

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';
   }

1 个答案:

答案 0 :(得分:0)

解决方案1 ​​

您可以使用file_get_contents

试试这个 -

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") ) );

解决方案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));