【发布时间】:2011-09-10 22:54:54
【问题描述】:
我正在尝试将凭据传递给一个网站,以便我可以在其上使用 file_get_contents 来提取一些数据,但它不起作用,我得到一个空白页面,所以知道这里出了什么问题吗?
<?php
$username="munged@ring.gil.com";
$password="Koin";
$url="confluence.rogersdigitalmedia.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
$str= file_get_contents("confluence.rogersdigitalmedia.com/display/prodsupport/Team+Calendar");
echo $str;
?>
这是新代码,当我获取内容时它仍然无法工作,卡在登录屏幕......
<?php
$username="munged@gil.ro.com";
$password="Koin";
$url="confluence.rogersdigitalmedia.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//Replaced due to special chars in url for username and pass
//curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_USERPWD, urlencode($username) . ':' . urlencode($password));
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
echo file_get_contents('http://confluence.rogersdigitalmedia.com/exportword?pageId=1114407');
?>
新代码:我知道$url 是我必须登录的URL,但我在$data 中输入了什么?我知道这是我的登录信息,但我该如何输入(例如,
<?php
function do_post_request($url, $data, $optional_headers = null)
{
$params = array('http' => array(
'method' => 'POST',
'content' => $data
));
if ($optional_headers !== null) {
$params['http']['header'] = $optional_headers;
}
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
if (!$fp) {
throw new Exception("Problem with $url, $php_errormsg");
}
$response = @stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from $url, $php_errormsg");
}
return $response;
}
【问题讨论】:
-
HTTP 响应标头是什么样的?你从服务器返回什么状态?