1

我正在为包含登录名的网站编写 API。我正在使用 cURL 来实现这一点。该站点需要先访问一个页面,然后才能访问另一个页面;例如:需要先打开“门户”,然后才能转到“门户/帐户信息”。正如预期的那样,当我使用 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");
?>

任何帮助是极大的赞赏。干杯!

4

1 回答 1

0

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

于 2013-10-27T10:27:12.233 回答