【问题标题】:PHP - Using cURL to get a cookie from a redirecting URL but getting no responsePHP - 使用 cURL 从重定向 URL 获取 cookie 但没有响应
【发布时间】:2018-03-07 21:40:21
【问题描述】:

我有一个 cURL 命令,我使用该命令对网站进行身份验证,它会提供一个 cookie 作为响应,然后使用此 cookie 我可以对该服务进行 REST API 调用

这是有效的 cURL 命令:

curl -v -l -H "Content-Type: application/x-www-form-urlencoded" -H "Referrer:https://mywebsiteurl.com?ticket=unique_ticket_id" -d "_charset_=UTF-8&errorMessage=User+name+and+password+do+not+match&resource=%2F&username=username%40domain.com&password=XXXXXX&nextpage=welcomeCM.jsp&viewInfo=&ticket=unique_ticket_id"  -X  POST https://mywebsiteurl.com/index.jsp

在上述命令中,ticket 参数包含一个唯一的ticket id,它与网站url 一起传递

现在,我正在尝试使用 cURL PHP 来完成同样的任务,这里是 PHP 代码:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://mywebsiteurl.com/index.jsp");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "_charset_=UTF-8&errorMessage=User+name+and+password+do+not+match&resource=%2F&username=username%40domain.com&password=XXXXXX&nextpage=welcomeCM.jsp&viewInfo=&ticket=unique_ticket_id");
curl_setopt($ch, CURLOPT_POST, 1);

$headers = array(
        'Content-Type: application/x-www-form-urlencoded',
        'Referrer: https://mywebsiteurl.com?ticket=unique_ticket_id'
        );
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}

echo $result;

curl_close ($ch);

这里echo $result 什么都没有显示

然后我尝试了var_dump($result);,它给出了以下输出:string(0) "",这意味着在$response 中没有返回任何内容

在此之后我尝试了curl_getinfo 并添加了以下代码:

echo "<pre>";
$info = curl_getinfo($ch);
print_r($info);
echo "</pre>";

这给了我以下数组:

Array
(
    [url] => https://mywebsiteurl.com/index.jsp
    [content_type] => text/html;charset=UTF-8
    [http_code] => 302
    [header_size] => 1306
    [request_size] => 619
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 1.540687
    [namelookup_time] => 0.028262
    [connect_time] => 0.171774
    [pretransfer_time] => 0.462507
    [size_upload] => 280
    [size_download] => 0
    [speed_download] => 0
    [speed_upload] => 181
    [download_content_length] => 0
    [upload_content_length] => 280
    [starttransfer_time] => 1.54066
    [redirect_time] => 0
    [redirect_url] => https://mywebsiteurl.com/welcomeCM.jsp?username=username@domain.com&locale=en_US
    [primary_ip] => YY.YYY.YYY.YY
    [certinfo] => Array
        (
        )

    [primary_port] => 443
    [local_ip] => XX.XX.XXX.XX
    [local_port] => 47692
)

现在我的目标是获取 $result 中的 cookie,但它是空的,并且没有返回任何内容

是不是 cURL 命令等效的 PHP 代码中缺少某些东西?

谁能帮助我并指出检索 cookie 的方向

非常感谢!

更新

我在请求中添加了curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);,让它在成功登录后跟随302重定向

但是这次cURL执行后,echo $result将我当前的本地网页重定向到/Dashboard?viewInfo=显然不存在

这一次var_dump($result); 的结果是:string(1462) " "

curl_getinfo($ch) 这次给出了以下数组:

Array
(
    [url] => https://mywebsiteurl.com/welcomeCM.jsp?username=username@domain.com&locale=en_US
    [content_type] => text/html;charset=UTF-8
    [http_code] => 200
    [header_size] => 1821
    [request_size] => 956
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 1
    [total_time] => 1.746911
    [namelookup_time] => 1.5E-5
    [connect_time] => 1.5E-5
    [pretransfer_time] => 6.2E-5
    [size_upload] => 0
    [size_download] => 1462
    [speed_download] => 836
    [speed_upload] => 0
    [download_content_length] => 1462
    [upload_content_length] => 0
    [starttransfer_time] => 0.146587
    [redirect_time] => 1.6003
    [redirect_url] => 
    [primary_ip] => YY.YYY.YYY.YY
    [certinfo] => Array
        (
        )

    [primary_port] => 443
    [local_ip] => YY.YY.YYY.YY
    [local_port] => 47705
)

这里仍然无法获取所需的cookie, 请帮忙!

【问题讨论】:

  • 一个可能无法解决您的问题但值得了解的注释:“Referrer”拼写错误,应该是“Referer”(是的,这是不正确的英文拼写,但事实就是这样在 HTTP 中)。
  • 您需要在请求中添加curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);,以便在成功登录后跟随302重定向。
  • @HaydenSchiff 是的,你是对的,确实是拼写错误,但在 HTTP 中就是这样 :)
  • @natheriel 在发布我的问题之前,我尝试了您共享的链接上给出的答案,但它对我不起作用,因为我的 URL 在执行时重定向,我将问题标题更改为强调相同
  • @dreamcoder007 好吧,只是为了确定。您的主要目标是使用 curl,在某个站点登录/验证自己 + 存储 cookie。之后您想使用 cookie 保持身份验证?

标签: php curl cookies


【解决方案1】:

如果你想使用 curls 给定的功能,你可以尝试使用 CURLOPT_COOKIEJAR / CURLOPT_COOKIEFILE 来存储和获取你的 cookie。如果基于文件。 在这种情况下,curl 会将任何与 Cookie 相关的信息写入文件中。

您可以定义此文件的位置。

通过使用 CURLOPT_COOKIEJAR ,您将告诉 curl 它将捕获 cookie 数据。

// create cookie file
$cookie = __DIR__ . "/where/you/want/to/store/your/cookie/mycookie.txt";
fopen($cookie, "w");
// you may want to use some random identicator in your cookie file
// to make sure it does not get overwritten by some action

// prepare login
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://mywebsiteurl.com/index.jsp");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// tell curl to follow redirects
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURlOPT_POSTFIELDS, "username=usernam&password=XXXXXX");
curl_setopt($ch, CURLOPT_POST, 1);
// set a jar, to store your cookie
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
$headers = array(
        'Content-Type: application/x-www-form-urlencoded',
        'Referrer: https://mywebsiteurl.com?ticket=unique_ticket_id'
        );
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// do login
$loginResult = curl_exec($ch);

// check if your login has worked according to the given result by any method you want
if (preg_match("/Welcome/", $loginResult)) {
    // simple regex for demonstration purpose
}

// now you could load your cookie out of the file, or just use the file for future requests

如果您的登录成功,您的所有信息都将存储在您的 cookie 文件中。

现在您可以再次使用该 cookie 和 curlopt CURLOPT_COOKIEFILE 来处理未来的请求。

// do other request, with your cookie
curl_setopt($ch, CURLOPT_URL, "https://mywebsiteurl.com/api/rest.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "your new post data");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
// set a jar to update your cookie if needed
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
// give curl the location of your cookie file, so it can send it
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);

【讨论】:

  • 感谢您的回复!我按照您的建议尝试了 CURLOPT_COOKIEJAR 。但是当我使用此代码运行 .php 页面时 - 它没有让我登录,而是显示mywebsiteurl.com/index.jsp 的登录页面,现在看来代码从网站上抓取了登录页面然后显示它。仍然无法获得所需的 cookie,请您帮忙
猜你喜欢
  • 1970-01-01
  • 2013-06-06
  • 1970-01-01
  • 1970-01-01
  • 2021-11-23
  • 1970-01-01
  • 2019-01-23
  • 2016-11-08
  • 1970-01-01
相关资源
最近更新 更多