繁体   English   中英

如何使用PHP和curl发布JSON?

[英]How can I post JSON with PHP and curl?

我设法使用以下设置在“ Advanced Rest Client”中处理我的请求:

在此处输入图片说明

但是,我尝试使用PHP重新创建相同的请求,但无法解决我做错了什么。 这是我所拥有的:

$config['key'] = "xxxxxxxxxxxx";
$config['clientid'] = xxxxxxxxx;

$url = 'http://xxxxxxxxxxxxxxxx.curdbee.com/invoices.json?api_token='.$config['key'];

$data = Array();  
$data['invoice']['client_id'] = $config['clientid'];

$data_string = json_encode($data);                                                                                   

$ch = curl_init($url);                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string))                                                                       
);                                                                                                                   

$result = curl_exec($ch);

echo curl_error($ch);

print_r($result);

$ result和curl_error都不会输出任何东西,我无法弄清楚原因。

我只需要添加以下两行:

curl_setopt($ ch,CURLOPT_SSL_VERIFYHOST,false);
curl_setopt($ ch,CURLOPT_SSL_VERIFYPEER,false);

尝试这个

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url);                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string))                                                                       
);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM