使用cURL和PHP登录Heroku时不起作用

使用cURL和PHP登录Heroku时不起作用,php,curl,heroku,Php,Curl,Heroku,我有一个简单的PHP脚本,可以登录到所需的网页。它在我的本地主机(wamp服务器)上运行正常,但当我在Heroku上运行它时,会得到如下响应(链接::) 脚本很简单,如下所示: <?php $username = "username"; $password = "password"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "url"); curl_setopt($ch, CURLOPT_POST, 1); curl_set

我有一个简单的PHP脚本,可以登录到所需的网页。它在我的本地主机(wamp服务器)上运行正常,但当我在Heroku上运行它时,会得到如下响应(链接::)

脚本很简单,如下所示:

<?php

$username = "username";
$password = "password";

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "url");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array( 'user[username]'=>$username, 'user[password]'=>$password)));
curl_setopt ($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

$content = curl_exec($ch);

echo $content;

?>


我不知道我做错了什么。是Heroku出了什么问题,还是很幸运代码在我的wamp服务器上运行正常?我还尝试在其他免费托管页面上运行此脚本,但它们不支持
CURLOPT\u FOLLOWLOCATION
参数,因此脚本无法运行。

页面顶部的文本是您请求的标题:

curl_setopt($ch,CURLOPT_头,true)

另外,
CURLOPT_POST
在使用
CURLOPT_POSTFIELDS
时是隐式的,不需要它

您的代码应该如下所示:

<?php

$username = "username";
$password = "password";

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://www.yoursite.com");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array( 'user[username]'=>$username, 'user[password]'=>$password)));
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

$content = curl_exec($ch);

echo $content;

看起来我试图发布到的页面有国家限制IP。

谢谢您的评论。我编辑了我的代码,但当我从Heroku(链接到我的应用:)运行时,它仍然将我转发到google站点,但它在我的wamp服务器上运行没有问题。也许有一些卷曲的设置?我正在从dropbox部署。@user1403588您在Heroku中解决了这个问题吗?我有一个问题,当我的应用程序在heroku运行时返回错误27。您好,由于页面提供了国家限制的IP,所以脚本无法工作,所以并没有什么可修复的。我的其他脚本按预期工作。