如何使用Kimono RESTful API更新参数?

时间:2015-10-01 15:30:10

标签: php rest curl kimono

具体来说,我希望更新要删除的网址。可以在此处找到文档:https://www.kimonolabs.com/apidocs#SetCrawlUrls

不幸的是,我对cURL和RESTful API的了解仅限于此。我最近的失败尝试是:

$ch = curl_init("https://kimonolabs.com/kimonoapis/");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json', 'kimonoapis/$api_id/update'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);

其中$ data是一个数组:

array(2) {
  ["apikey"]=>
  string(32) "API_KEY"
  ["urls"]=>
  array(2) {
    [0]=>
    string(34) "URL 1"
    [1]=>
    string(34) "URL 2"
  }
}

我还尝试了json_encode的变体,传递了查询字符串中的参数,以及cURL的不同变体,但到目前为止还没有成功。您如何成功利用其RESTful API?

2 个答案:

答案 0 :(得分:3)

由于您使用单引号,因此未解释变量$api_id

示例:

<?php

$var = "api";

var_dump(array('$api'));

输出:

array(1) { [0]=> string(4) "$api" }

相关阅读:What is the difference between single-quoted and double-quoted strings in PHP?

尝试更改行:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json', 'kimonoapis/$api_id/update'));

使用双引号,或连接$api_id变量'kimonoapis/' . $api_id . '/update'

更新

由于API需要JSON,您应该这样做:

$payload = json_encode( array('api_key' => 'key', 'urls' => array('url1', 'url2' ) );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );

根据手册If value is an array, the Content-Type header will be set to multipart/form-data.使用数组时 因此400错误。

更新2:

$ch = curl_init("https://kimonolabs.com/kimonoapis/");
$data = json_encode(array('apikey' => 'yourkey', 'urls' => array('url1', 'url2')));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json', 'kimonoapis/' . $api_id . '/update'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);

答案 1 :(得分:0)

$array = array('apikey' => 'API_KEY', 'urls' => array('URL_1', 'URL_2'));
$postvars = http_build_query($array);
$url = "https://kimonolabs.com/kimonoapis/{API_ID}/update";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);
$result = curl_exec($ch);
curl_close($ch);

经过更多的追踪,错误和谷歌这是我最终的工作。感谢所有帮助@JohnSvensson