【问题标题】:cURL PHP Domain Defaults to LocalhostcURL PHP 域默认为 Localhost
【发布时间】:2014-12-08 09:31:18
【问题描述】:

我正在请求一个带有自动登录的 cURL 的站点,尽管脚本登录后的每个 GET 请求都可以正常运行,但 URL 中将包含“localhost”而不是站点的实际域,因此它会抛出一个 @ 987654325@未找到错误。

示例:不是https://remotesite.com/dashboard,而是http://localhost.com/dashboard (另请注意,http://localhost.com/dashboard 没有使用 https)

这是我的代码:

$curl = curl_init();

$user_agent = 'Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0';
$url = 'https://remotesite.com/dashboard';
$post_data = 'username=username&password=password&cookie=true&destination=&login=Log+In';

curl_setopt ($curl, CURLOPT_URL, $url);
curl_setopt ($curl, CURLOPT_USERAGENT, $user_agent);
curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt ($curl, CURLOPT_HEADER, 0);
curl_setopt ($curl, CURLOPT_POSTFIELDS, $post_data);
curl_setopt ($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($curl, CURLOPT_AUTOREFERER, 1);
curl_setopt ($curl, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt ($curl, CURLOPT_COOKIEJAR, "cookie.txt");

$result = curl_exec ($curl);
$errmsg  = curl_error($curl);

echo $result;

【问题讨论】:

  • 如果是post请求,你应该将CURLOPT_POST设置为TRUE。根据您的 PHP 版本,默认情况下它可能是 FALSE。看看有没有效果。
  • @Max 试过但没用 :(
  • 你在代理后面吗?
  • @Max 不知道,我在工作中使用网络。
  • 您使用的是 Linux 还是 Windows?可能是因为您使用的是办公室互联网连接

标签: php curl https http-status-code-404


【解决方案1】:

发生这种情况是因为您尝试访问的站点在其 href 中使用了亲属路径。 因此,您应该尝试更改 base tag 以使用正确的域。

这个问题可能会对你有所帮助:Curl and relative path in <head>

【讨论】:

    猜你喜欢
    • 2013-08-05
    • 2012-05-05
    • 2021-11-27
    • 2018-07-27
    • 1970-01-01
    • 2011-04-22
    • 2011-10-16
    • 2014-10-22
    • 2014-01-16
    相关资源
    最近更新 更多