【问题标题】:How do I add an array to a url or send a post array?如何将数组添加到 url 或发送 post 数组?
【发布时间】:2011-09-20 12:24:14
【问题描述】:

好的,我想提出一个 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 脚本文件中有 session_start() 吗?
  • 是的,我做 session_set_cookie_params(0, '/'); session_start();

标签: php session post curl


【解决方案1】:

尝试:

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);

【讨论】:

  • 所以我尝试了 curl_setopt($ch, CURLOPT_POSTFIELDS, array('items' => implode(",", $_SESSION['cart'])));但它只返回 [items] => 1,1 这是我需要的一半
【解决方案2】:

试试这个

$post = "";
   $amp = "";
   foreach($_SESSION['cart'] as $key => $value) {
       $post .= $amp . $key . "=" . urlencode($value);
       $amp = "&";                        
   }
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

【讨论】:

    【解决方案3】:

    尝试使用php的序列化和反序列化功能

    http://php.net/manual/en/function.serialize.php

    【讨论】:

      猜你喜欢
      • 2017-07-12
      • 1970-01-01
      • 2016-01-23
      • 2021-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-20
      相关资源
      最近更新 更多