繁体   English   中英

PHP Basecamp API“ PUT”中的cURL

[英]cURL in PHP Basecamp API “PUT”

我正在尝试通过新的Basecamp Api编辑现有的Basecamp项目。 我收到此错误:

lexical error: malformed number, a digit is required after the minus sign. ---------------      ---------------6 (right here) ------^

我的代码:

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_URL, 'https://basecamp.com/****/api/v1/projects/****.json');
curl_setopt($ch, CURLOPT_USERAGENT, "User-Agent : Holy Grail (user@example.com)");
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, array("name" => "from cURL"));



$result = curl_exec($ch);
echo $result;
curl_close($ch);


if ($result == false) {
echo "Fetch failed" ;
}
else {
$obj = json_decode($result, true);
}

//var_dump($obj);



?>

我确定我只是在做一些愚蠢的事情,但是任何帮助都值得赞赏。

谢谢!

更新我现在所拥有的:

$username = 'user';
$password = 'pass';
$data = json_encode(array("name" => "from cURL"));

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_URL, 'https://basecamp.com/****/api/v1/projects/*****.json');
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);     
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_PUT, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'User-Agent : Holy Grail     (user@example.com)');
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                                              'Content-Type :application/json',
                                              'Content-Length: ' .strlen($data)));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);





$result = curl_exec($ch);
echo $result;
curl_close($ch);


if ($result == false) {
echo "Fetch failed" ;
}
else {
$obj = json_decode($result, true);
}

//var_dump($obj);



?>
</body>
</html>

BasecampAPI仅接受JSON数据,您可以在-d参数中看到-

curl -u username:password \
  -H 'Content-Type: application/json' \
  -H 'User-Agent: MyApp (yourname@example.com)' \
  -d '{ "name": "My new project!" }' \
  https://basecamp.com/999999999/api/v1/projects.json

因此,您不会在此行中发送JSON数据-

curl_setopt($ch, CURLOPT_POSTFIELDS, array("name" => "from cURL"));

删除CUSTOMREQUEST选项并添加CURLOPT_PUT 将您的代码修改为-

$data_string = json_encode(array("name" => "from cURL"));
...

curl_setopt($ch, CURLOPT_HEADER, 1);   
curl_setopt($ch, CURLOPT_PUT, True);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
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