【问题标题】:Passing value in postfield. PHP在 postfield 中传递值。 PHP
【发布时间】:2017-09-10 07:00:14
【问题描述】:

这是我想传入 CURLOPT_POSTFIELDS 的参数

{"user":{"user_id":"346"}}    

示例:

curl_setopt($ch, CURLOPT_POSTFIELDS, {"user":{"user_id":"346"}});

我该怎么做?

谢谢

【问题讨论】:

标签: php arrays curl


【解决方案1】:

根据您的示例,您的数据是json,因此您需要将数据传递为json string

$data_string = '{"user":{"user_id":"346"}}';
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);

同样,当你传递 json 时,将 header 设置为 json application

curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string))                                                                       
);  

【讨论】:

  • 不要手动设置 Content-Length 标头,curl 会自动为您设置。和你不同,curl 不会打错字
  • 顺便说一句,如果你想从原生 php 数据中对其进行编码,它是 json_encode(array('user'=>array('user_id'=>346)))
猜你喜欢
  • 2012-07-09
  • 1970-01-01
  • 1970-01-01
  • 2017-02-02
  • 2011-05-29
  • 1970-01-01
  • 1970-01-01
  • 2013-03-20
  • 1970-01-01
相关资源
最近更新 更多