1

我正在尝试通过新的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>
4

1 回答 1

1

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))                                                                       
);                 
于 2013-03-20T10:10:03.737 回答