douge7771
2014-10-30 16:12
浏览 64
已采纳

php-curl无法正常工作?

I am trying to send POST requests to Digitalocean's API v2 with cURL and custom headers but it is not working. I don't get any response, or the output/response I get is only:

Response from API:

My php code is:

<?php
$TOKEN = "digitalocean api token";

$headers = array("Authorization: Bearer $TOKEN","Content-Type: application/json",);

 $name = "Test"; //droplet name
 $region = "nyc2"; //region
 $size = "512mb"; //size
 $image = "303022"; //replace it
 $user_data = "#!/bin/bash apt-get install nginx -y";

$postData = array(
'name' => $name,
'region' => $region,
'size' => $size,
'image' => $image,
'user_data' => $user_data,);

$post_body = '';
foreach($postData as $key => $value) {
$post_body.= urlencode($key) . '=' . urlencode($value) . '&';
}
$post_body = rtrim($post_body, '&');

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, "https://api.digitalocean.com/v2/droplets");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_body);
$result = curl_exec($ch);

echo "Response from API: $result";
?>

Please tell me what's wrong in it? I have error reporting enabled.

图片转代码服务由CSDN问答提供 功能建议

我正在尝试使用cURL和自定义标头向Digitalocean的API v2发送POST请求,但它无法正常工作。 我没有得到任何响应,或者我得到的输出/响应只是:

来自API的响应:

我的php代码是:

 &lt;?php 
 $ TOKEN =“digitalocean api token”; 
 
 $ headers = array(“授权”  :Bearer $ TOKEN“,”Content-Type:application / json“,); 
 
 $ name =”Test“;  // droplet name 
 $ region =“nyc2”;  // region 
 $ size =“512mb”;  // size 
 $ image =“303022”;  //替换它
 $ user_data =“#!/ bin / bash apt-get install nginx -y”; 
 
 $ postData = array(
'name'=&gt; $ name,
'region'  =&gt; $ region,
'size'=&gt; $ size,
'image'=&gt; $ image,
'user_data'=&gt; $ user_data,); 
 
 $ post_body =''  ; 
foreach($ postData as $ key =&gt; $ value){
 $ post_body。= urlencode($ key)。  '='。  urlencode($ value)。  '&amp;'; 
} 
 $ post_body = rtrim($ post_body,'&amp;'); 
 
 $ ch = curl_init(); 
curl_setopt($ ch,CURLOPT_HTTPHEADER,$ headers); 
curl_setopt(  $ ch,CURLOPT_URL,“https://api.digitalocean.com/v2/droplets");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 
curl_setopt($ ch,CURLOPT_POST,1); 
curl_setopt($ ch,  CURLOPT_POSTFIELDS,$ post_body); 
 $ result = curl_exec($ ch); 
 
echo“来自API的响应:$ result”; 
?&gt; 
   
 
 < 请告诉我它有什么问题? 我启用了错误报告。 
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dt97868 2014-10-30 16:24
    已采纳

    After looking at the $headers array more closely, I noticed you're sending your content as application/json, but at the same time, the postfields value does not match that content type. You'll probably have to add this:

    $postFields = json_encode($postData);
    $headers[] = 'Content-Length: ' . strlen($postFields));
    //set your opts
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);//json encoded string
    

    If that doesn't work, add the result of

    var_dump(curl_getinfo($ch));
    

    To your question, that'll tell you everything there is to know about the status of your curl request.

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题