使用curl发布数据 - 实际上是刷新到下一页 [英] Posting data with curl - actually refreshing to the next page

查看:238
本文介绍了使用curl发布数据 - 实际上是刷新到下一页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在发布讯息的网页之间传送讯息资料。不是一个表单 - 像我可能在页面或某事之间传递验证错误(使用它在几个地方)。

I'm trying to send post data between pages with Post. Not a form - like I may be passing validation error between pages or something (using it in several places).

cURL执行正常,但它只是在新页面底部。如何执行cURL发布并加载以下页面?

The cURL is executing fine, but it's just tacking on the new page at the bottom. How can I execute a cURL Post and load the following page?

所以我的目标是在页面之间发送数据。我不想使用GET或cookies,因为我不想依赖用户,我不想使用$ _SESSION,因为它不是直接关于会话的发送私人数据之间的页面。

So my goal is to send data between pages. I do not want to use GET or cookies as I do not want to rely on the user, and I'd prefer not to use $_SESSION as it is not so much directly about a session as sending private data between pages.

感谢

$ch = curl_init($some_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'myvar=something');
curl_exec($ch);
curl_close($ch);

我怀疑代码有任何相关性,因为它是关于执行任务而不是代码语法

I doubt the code has any relevance, as it's about performing a task not code syntax (there may be one in this example but you'll have to trust me it's not the code that's buggy as it's retrieving fine).

推荐答案

你可以使用这个方法可以将 $ _ SESSION 超全局变量分成描述当前用户/会话和表单创建的任何错误的数组。例如,

You could separate your $_SESSION superglobal into arrays describing both the current user/session and any errors that your form has created. For instance,

$_SESSION = array(
  'user' => array(), // user data goes here
  'errors' => array() // validation data goes here
);

if (!$_POST['myvar'] == 'something') {
  $_SESSION['errors']['myvar'] = 'You must specify a value for <code>myvar</code>';
}

然后,您可以使用类似的调用输出错误this:

You would then be able to output errors on subsequent pages using a call something like this:

if (isset($_SESSION['errors'])) {
  foreach($_SESSION['errors'] as $error) {
    echo '<li>' . $error . '</li>';
  }
}

这篇关于使用curl发布数据 - 实际上是刷新到下一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆