doq8211 2015-11-20 09:37
浏览 60

PHP | 在AJAX中使用Curl POST变量

How to replicate this data post by ajax with curl?

  ...
  data: {
    search: JSON.stringify({
    daterange : "custom", 
    daterangecustom  : {
        start   : "2013-03-01T23:59:59",
        end     : "2018-03-01T23:59:59"
      }
    }),
    limit: 50
  },
  ...

I dont need the whole code for curl, but only $params value:

curl_setopt($curl, CURLOPT_POSTFIELDS, $params);

I`ve been trying to make this work for a while, with no success. Thanks a lot to anyone with a solution here!!

  • 写回答

1条回答 默认 最新

  • drblhw5731 2015-11-20 18:15
    关注

    Without knowing a little more, I think this is what you are looking for:

    $data = array(
        'search' => array(
            'daterange' => 'custom',
            'daterangecustom' => array(
                'start' => '2013-03-01T23:59:59',
                'end'   => '2018-03-01T23:59:59',
            ),
        ),
        'limit' => 50,
    );
    
    $headers = array(
        'Content-type: application/json',
    );
    
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    

    The request will look like this:

    POST /url HTTP/1.1
    Host: host
    Accept: */*
    Content-type: application/json
    Content-Length: 122
    
    {"search":{"daterange":"custom","daterangecustom":{"start":"2013-03-01T23:59:59","end":"2018-03-01T23:59:59"}},"limit":50}
    
    评论

报告相同问题?

悬赏问题

  • ¥15 C# P/Invoke的效率问题
  • ¥20 thinkphp适配人大金仓问题
  • ¥20 Oracle替换.dbf文件后无法连接,如何解决?(相关搜索:数据库|死循环)
  • ¥15 数据库数据成问号了,前台查询正常,数据库查询是?号
  • ¥15 算法使用了tf-idf,用手肘图确定k值确定不了,第四轮廓系数又太小才有0.006088746097507285,如何解决?(相关搜索:数据处理)
  • ¥15 彩灯控制电路,会的加我QQ1482956179
  • ¥200 相机拍直接转存到电脑上 立拍立穿无线局域网传
  • ¥15 (关键词-电路设计)
  • ¥15 如何解决MIPS计算是否溢出
  • ¥15 vue中我代理了iframe,iframe却走的是路由,没有显示该显示的网站,这个该如何处理