简体   繁体   English

如何使用PHP和curl发布JSON?

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

I managed to get my request working in "Advanced Rest Client" using the following settings: 我设法使用以下设置在“ Advanced Rest Client”中处理我的请求:

在此处输入图片说明

However, I tried to recreate the same request using PHP and I can't work out what I'm doing wrong. 但是,我尝试使用PHP重新创建相同的请求,但无法解决我做错了什么。 Here's what I have: 这是我所拥有的:

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

Neither $result nor curl_error outputs anything and I can't work out why. $ result和curl_error都不会输出任何东西,我无法弄清楚原因。

I just needed to add these two lines: 我只需要添加以下两行:

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

Try this 尝试这个

$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