doukou4066 2013-03-19 19:33
浏览 39
已采纳

PHP Basecamp API“PUT”中的cURL

I am trying to edit an existing Basecamp project via the new Basecamp Api. I am receiving this error:

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

My Code:

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



?>

I'm sure I'm just doing something stupid, but any help is appreciated.

Thanks!

UPDATE What I have now:

$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>
  • 写回答

1条回答 默认 最新

  • duan010167787 2013-03-20 10:10
    关注

    BasecampAPI accepts only JSON data, you can see here in -d parameter -

    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
    

    So you're not sending JSON data in this line -

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

    Remove the CUSTOMREQUEST option and add CURLOPT_PUT. Modify your code to -

    $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))                                                                       
    );                 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度