使用 php cUrl 发送会话变量
我正在尝试在我的应用程序内的脚本之间发送数据。
问题是会话 ID 没有响应。
脚本 1 是...
<?php
session_start();
$_SESSION['id'] = 1;
$data = "data to be sent to script";
$ch = curl_init("http:.../myscript.php");
$nvp = "&data=$data";
curl_setopt($ch, CURLOPT_POSTFIELDS, $nvp);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
echo curl_exec($ch);
?>
myScript.php 是...
<?php
session_start();
$id = $_SESSION['id'];
$data = $_POST['data'];
$result = function idData($id, $data); // returns matching results.
echo "Session Id = $id <br />";
echo "Id result = $result <br />";
?>
但是 myScript.php 无法正常访问会话数据。
有解决办法吗?可能的原因是什么?
谢谢
Am trying to send data between scripts within my application.
Problem is session id is not responding.
Script 1 is...
<?php
session_start();
$_SESSION['id'] = 1;
$data = "data to be sent to script";
$ch = curl_init("http:.../myscript.php");
$nvp = "&data=$data";
curl_setopt($ch, CURLOPT_POSTFIELDS, $nvp);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
echo curl_exec($ch);
?>
myScript.php is...
<?php
session_start();
$id = $_SESSION['id'];
$data = $_POST['data'];
$result = function idData($id, $data); // returns matching results.
echo "Session Id = $id <br />";
echo "Id result = $result <br />";
?>
However myScript.php is not able to access the session data as per normal.
Is there a work around for this? What could the possible cause be?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我相信您正在寻找 CURLOPT_COOKIE
I believe you're looking for CURLOPT_COOKIE
在脚本 1 中,如果您自己跟踪响应中的会话 ID,则可以使用
CURLOPT_COOKIE
。我认为如果脚本 1 将向 myscript.php 发出多个请求来创建会话,则您不需要或不希望在脚本 1 中使用 session_start 。
在脚本 1 中使用它:
然后像平常一样提出您的请求。请求完成时,myscript.php 设置的任何 cookie 都将保存到 cookie jar 文件中,在发送请求之前将检查 cookie 文件中是否有要发送的 cookie。
您可以手动跟踪来自curl 请求的php 会话cookie,并使用
CURLOPT_COOKIE
。In script 1 you could use
CURLOPT_COOKIE
if you keep track of the session ID from the response yourself.I don't think you need or want session_start in script 1 if it is going to be making multiple requests to myscript.php that creates a session.
Use this in script 1:
And then make your request as normal. Any cookies set by myscript.php will be saved to the cookie jar file when the request completes, the cookiefile will be checked for any cookies to be sent prior to sending the request.
You could manually track the php session cookie from the curl request and use
CURLOPT_COOKIE
as well.您错过了一个使用 post 的选项参数。请添加这个,它应该可以工作:
curl_setopt($ch, CURLOPT_POST, true);
You miss one option parameter for using post. Please add this one, it should work:
curl_setopt($ch, CURLOPT_POST, true);