cURL PHP网站Passthrough与CookieJar

时间:2013-10-27 02:44:31

标签: php bash curl

我正在为包含登录信息的网站编写API。我正在使用cURL来实现这一目标。该站点需要访问一个页面才能访问另一个页面;例如:在我进入“Portal / AccountInfo”之前,需要打开“Portal”。正如预期的那样,当我使用Bash手动编写脚本时,我在调用/ Portal / AccountInfo时获得所需的输出。

curl -c c.txt -d "Username=u&Password=p" https://somewebsite.com/Login
curl -b c.txt -L https://somewebsite.com/Portal/
curl -b c.txt -L https://somewebsite.com/Portal/AccountInfo/

虽然在使用cURL将其转换为PHP时,我无法获得第三个请求的正确输出。见下文:

<?php

error_reporting(E_ALL);
ini_set("display_errors", 1);

$Username = $_POST["Username"];
$Password = $_POST["Password"];

$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/cookie");
curl_setopt($ch, CURLOPT_URL,"https://somewebsite.com/Login");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "Username=$Username&Password=$Password");
curl_exec ($ch);

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/cookie");
curl_setopt($ch, CURLOPT_URL,"https://somewebsite.com/Portal");
curl_exec ($ch);
curl_close ($ch);

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/cookie");
curl_setopt($ch, CURLOPT_URL,"https://somewebsite.com/Portal/AccountInfo");
$buf3 = curl_exec ($ch);
curl_close ($ch);

echo "<PRE>".htmlentities($buf3); //This output is not the same as the third command I wrote in bash
unlink("/tmp/cookie");
?>

非常感谢任何帮助。干杯!

1 个答案:

答案 0 :(得分:0)

您尚未将-L转换为PHP代码:CURLOPT_FOLLOWLOCATION,它可能是缺少的。