0

Specifically I am looking to update the URLs that are to be scraped. Documentation can be found here: https://www.kimonolabs.com/apidocs#SetCrawlUrls

Unfortunately my knowledge about cURL and RESTful APIs is limited to say the least. My most recent failed attempt was:

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

where $data is an array of:

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

I have also tried variations of json_encode, passing the parameters in the query string, and different variations of cURL but have not be successful thus far. How do you successfully take advantage of their RESTful API?

fwho
  • 248
  • 2
  • 14

2 Answers2

3

The variable $api_id is not being interpreted because you are using single-quotes.

Example:

<?php

$var = "api";

var_dump(array('$api'));

Outputs:

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

Related read: What is the difference between single-quoted and double-quoted strings in PHP?

Try changing the line:

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

To use double-quotes, or concatenate the $api_id variable 'kimonoapis/' . $api_id . '/update'

Update:

Since the API expects JSON, you should do this:

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

When using an array as you do, according to the manual If value is an array, the Content-Type header will be set to multipart/form-data. Hence the 400 error.

Update 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);
Community
  • 1
  • 1
John Svensson
  • 392
  • 1
  • 6
  • Good catch! I completely overlooked that. Unfortunately I still get http_code error 400. – fwho Oct 01 '15 at 15:51
  • http_code 404 when changing to multipar/form-data. Adding json_encode $data to the HTTPHEADER gives code 200, but does not seem to change the URLs. – fwho Oct 01 '15 at 16:11
  • You shouldn't change to multipart/form-data it should be as-is. curl_setopt($ch, CURLOPT_POSTFIELDS, $data), $data here should be the json_encoded of the array you had earlier. – John Svensson Oct 01 '15 at 16:12
  • Still 404 "Cannot POST /kimonoapis/" and removing that from init gives 404 "Not Found" – fwho Oct 01 '15 at 16:19
  • Are you sure "https://kimonolabs.com/kimonoapis/" is the correct URL? Shouldn't it be "https://kimonolabs.com/api/", "https://kimonolabs.com/apis/" or just "https://kimonolabs.com/" – John Svensson Oct 01 '15 at 16:28
  • That came from here: https://help.kimonolabs.com/hc/en-us/articles/203276194-Tutorial-Start-crawls-via-the-REST-API /api/ gives "Cannot POST /api/" /apis/ and / gives "Not Found" – fwho Oct 01 '15 at 16:33
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/91120/discussion-between-john-svensson-and-fwho). – John Svensson Oct 01 '15 at 16:36
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);

After a lot more trail, error, and Google this is what I finally got to work. Thanks for all the help @JohnSvensson

fwho
  • 248
  • 2
  • 14