繁体   English   中英

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

[英]PHP. Unable to consume remote web service using WAMP server

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

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

下面是我的代码。

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

您可以使用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));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM