具体来说,我期待更新的网址,将被刮。文档可以在这里找到: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?
发布于 2015-10-07 22:27:14
$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
发布于 2015-10-01 15:47:01
变量$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);https://stackoverflow.com/questions/32891432
复制相似问题