无法在 cURL PHP 中使用 cookie

发布于 2024-09-25 19:11:09 字数 1654 浏览 4 评论 0 原文

我正在使用 cURL 来解析网站。

http://www.hcmiu.edu.vn/bookforsale/showbooks.php< /a>

它需要会话才能查看,如果没有会话,则页面重定向到:

http://www.hcmiu.edu.vn/bookforsale/perInfo.php< /a>

我使用此代码来获取会话 cookie,但我不知道为什么我看不到文件 cookies.txt 的任何更改,

$urltopost = "http://www.hcmiu.edu.vn/bookforsale/perInfo.php";
$datatopost = array (
"name" => "abc",
"tel" => "99999999",
"email" => "[email protected]",
"profession" => "abc",
"employer" => "abc",
"tel_work" => "99999999",
); 
$ch = curl_init($urltopost);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $datatopost);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/cookies.txt"); 

$returnData = curl_exec($ch);
$file = fopen("example.txt", "w");
fwrite($file, $returnData);
fclose($file);
curl_close($ch);

看到:

Set-Cookie: PHPSESSID=1egb993objkrdp3gi5mpvs02g0; path=/ 

但是,我在标头中 。感谢您的帮助

-编辑: 我使用了一个棘手的方法:我使用http查看器来查看浏览器cookie中的PHPSESSID。然后我用它创建一个 cookies 文件以供 cURL 读取。然后我就可以通过网络服务器的会话检查来查看文件 showbooks.php 。

I'm using cURL to parse a website.

http://www.hcmiu.edu.vn/bookforsale/showbooks.php

It need session to view, if you don't have session then the page redirect to :

http://www.hcmiu.edu.vn/bookforsale/perInfo.php

I use this code to get session cookie but I don't know why I cannot see any change to file cookies.txt

$urltopost = "http://www.hcmiu.edu.vn/bookforsale/perInfo.php";
$datatopost = array (
"name" => "abc",
"tel" => "99999999",
"email" => "[email protected]",
"profession" => "abc",
"employer" => "abc",
"tel_work" => "99999999",
); 
$ch = curl_init($urltopost);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $datatopost);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/cookies.txt"); 

$returnData = curl_exec($ch);
$file = fopen("example.txt", "w");
fwrite($file, $returnData);
fclose($file);
curl_close($ch);

However, I see:

Set-Cookie: PHPSESSID=1egb993objkrdp3gi5mpvs02g0; path=/ 

in the header. Thanks for any help

-Edit:
I use a tricky way: I use http viewer to view the PHPSESSID in browser cookies. And then I use it to to create a cookies file for cURL to read. Then I could pass the session checking of the web server for viewing file showbooks.php .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

哥,最终变帅啦 2024-10-02 19:11:10

我使用此代码片段从服务器的响应中提取 cookie:

$returnData = curl_exec($ch);
$cookie = '';
$pattern = '/Set-Cookie:(.*?)\n/';
if (preg_match($pattern, $returnData, $result))
$cookie = $result[1];

我已在 这篇文章。此方法不使用 cookie 文件。


关于 cookie 文件,请考虑以下来自 http:// 的建议php.net/manual/en/book.curl.php

使用绝对路径来设置
变量 CURLOPT_COOKIEFILE &
CURLOPT_COOKIEJAR。为了让生活更轻松
使用 realpath("file.txt") 函数
获取绝对路径。

I used this code snippet to extract the cookie from the server's response:

$returnData = curl_exec($ch);
$cookie = '';
$pattern = '/Set-Cookie:(.*?)\n/';
if (preg_match($pattern, $returnData, $result))
$cookie = $result[1];

I've described my experience in this post. This approach doesn't use cookie file.


What concerns cookie file, consider the following advice from http://php.net/manual/en/book.curl.php:

Use absolute path for setting the
variables CURLOPT_COOKIEFILE &
CURLOPT_COOKIEJAR. To make life easier
use the realpath("file.txt") function
to get the absolute path.

生死何惧 2024-10-02 19:11:10

文件可写吗?它真的存在吗?

is the file writable? does it even exist?

又怨 2024-10-02 19:11:10

尝试创建该文件并赋予其全局读写权限。

还可以尝试使用相对路径(例如 ./cookies.txt)而不是 cookie jar 文件的绝对路径。

Try creating the file and give it global read write privilege.

Also try with a relative path (say, ./cookies.txt) instead of an absolute path for the cookie jar file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文