我有一个提示输入用户名/密码的URL来获取其数据。数据采用XML格式。起初我试图检索这样的数据:
$url = "http://username:password@[ip-address]:8086/connectioncounts?flat";
$xml = simplexml_load_file($url);
print_r($xml);
在搜索完答案后,我想出了这段代码:
$url = "http://[ip-address]:8086/connectioncounts?flat";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "username:password");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
我没有在这些尝试中返回任何数据。有人能告诉我我做错了吗?
答案 0 :(得分:1)
我觉得网址是重定向的,您可以尝试添加。这将按照URL返回响应。
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
答案 1 :(得分:1)
您需要通过填充数组中的CURL选项来更改URL中的端口 -
$ch = curl_init();
$options = array(CURLOPT_URL => "http://[ip-address]/connectionscounts?flat/<URI>",
CURLOPT_PORT => "8086",
CURLOPT_HEADER => "Content-Type:application/xml",
CURLOPT_USERPWD => "username:password",
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_RETURNTRANSFER => TRUE
);
curl_setopt_array($ch, $options);
$data = curl_exec($ch);
$xml = new SimpleXMLElement($data);
print_r($xml);