php - 如何将数组添加到 url 或发送 post 数组?

标签 php session post curl

好的,所以我有一个我正在尝试提出的 curl 请求。

$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://someurl.com/shop_pos/index.php?route=module/cart/ajax_get_all_products");

// Do a POST
$items = 
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $_SESSION['cart']);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$result = curl_exec($ch);
curl_close($ch);
echo $result;

内容或$_SESSION['cart']
Array
(
  [155] => 1
  [78] => 1
)

我需要将此数据作为发布数据或获取变量发送...任何建议

关于捕捉功能
public function ajax_get_all_products(){
    $this->language->load('module/cart');
    $all_products = $this->cart->getProducts();
    $data_returned = json_encode($all_products);
    echo "<pre>".print_r($_REQUEST, true)."</pre>";
    echo "<pre>".print_r($_POST, true)."</pre>";
    $this->response->setOutput($data_returned, $this->config->get('config_compression'));
}

我没有得到我设置的数组。顺便说一下,这两个文件在同一台服务器上

最佳答案

尝试:

curl_setopt($ch, CURLOPT_POSTFIELDS, array('items' => $_SESSION['cart']));

尝试这个:
$post_data = http_build_query(array('items' => $_SESSION['cart']));
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

关于php - 如何将数组添加到 url 或发送 post 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6442558/

相关文章:

C# session "Object reference not set to an instance of an object."

python - iOS 不发送 POST 请求负载

php - 如何在 magento 中以编程方式将产品及其自定义选项信息保存到另一个表

php - MySQL更新问题

django - 如何将 session 数据放入 SetUp() - Django 测试

java - Java EE 应用程序的 session 处理

python - 使用 Python 发送数千个 post 请求的最快方法是什么?

javascript - 为什么 AngularJS 在 $http post 请求中的数据对象末尾添加冒号

php - 我们如何读取laravel中views文件夹中存储的html文件的内容?

php - Mysql 查询返回错误 #1093 - 您无法指定目标表