我正在尝试让 curl 输出。它是在该地址输出的基本文本字符串。
我正在执行此代码,但它不起作用,但如果我取出 27017 并加载主域,则可以正常工作:
$cookie_file_path = "cookiejar.txt";
$fp = fopen("$cookie_file_path","w");
fclose($fp);
$LOGINURL = "http://www.myserver.org:27017";
$agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322)";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$LOGINURL);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
$result = curl_exec ($ch);
echo $result;
知道为什么它不起作用吗?任何帮助将不胜感激。不确定它是否相关,但 Web 服务器与在 27017 端口上输出的应用程序在同一台机器上运行。
更新:
$cookie_file_path = "cookiejar.txt";
$username = "myusername";
$password = "mypassword";
$LOGINURL = "http://www.myserver.org";
$agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322)";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$LOGINURL);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_PORT , "27017");
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
$result = curl_exec ($ch);
echo $result;
更新 2:
解决。
事实证明,对于这个特定实例,它需要摘要身份验证。
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
解决了这个问题...
感谢 Barmar 让我意识到它确实需要用户/通行证。