PHP。无法使用WAMP服务器使用远程web服务
我正在使用WAMP服务器,并尝试在远程web服务中进行读取。我收到以下错误- 警告: simplexml\u加载\u文件(): 无法打开流:HTTP请求失败!HTTP/1.1400错误请求 在第7行的C:\wamp\www\php\ws.php中 下面是我的代码PHP。无法使用WAMP服务器使用远程web服务,php,wamp,Php,Wamp,我正在使用WAMP服务器,并尝试在远程web服务中进行读取。我收到以下错误- 警告: simplexml\u加载\u文件(): 无法打开流:HTTP请求失败!HTTP/1.1400错误请求 在第7行的C:\wamp\www\php\ws.php中 下面是我的代码 if( ! $xml = simplexml_load_file('http://example.com/search-api/search/devapi/coupons?format=xml&key=xxxxxxxx&
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 您可以使用
文件获取内容
试试这个-
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));
联系
atti.com
支持人员,询问他们您的请求到底有什么问题。您收到的错误表示您的xml url不存在或无效。尝试将url放在浏览器中,检查它是否为有效的xml。它是有效的xml,并显示在我的浏览器中。我正在用我的api密钥替换“xxxxxxx”。我尝试了代码(print_r(simplexml_load_string(file_get_contents(“)));但它没有显示任何内容。这很有效。有没有办法解析它,将其回显出来?我正在尝试…如果(!$xml=simplexml_load_string($result)){echo“无法加载xml文件”;}其他方法{//echo'XML文件加载成功;//print_r($XML);echo$XML->searchResult->searchListings->searchListing[0];}无需担心。如果(!$XML=simplexml\u load_string($result)){echo'无法加载XML文件';}否则{//echo'XML文件加载成功
;//print_r($XML);echo$xml->searchListings->searchListing[0]->businessName;}