如何使用Kimono RESTful API 更新参数?
How do you use the Kimono RESTful API to update parameters?
具体来说,我希望更新要抓取的 URL。可以在此处找到文档:https://www.kimonolabs.com/apidocs#SetCrawlUrls
不幸的是,我对 cURL 和 RESTful APIs 的了解至少可以说是有限的。我最近一次失败的尝试是:
$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?
变量 $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);
$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);
经过更多的跟踪、错误和 Google 这就是我终于开始工作的地方。感谢所有帮助@JohnSvensson
具体来说,我希望更新要抓取的 URL。可以在此处找到文档:https://www.kimonolabs.com/apidocs#SetCrawlUrls
不幸的是,我对 cURL 和 RESTful APIs 的了解至少可以说是有限的。我最近一次失败的尝试是:
$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?
变量 $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);
$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);
经过更多的跟踪、错误和 Google 这就是我终于开始工作的地方。感谢所有帮助@JohnSvensson