0

I recently work with kraken.io API and I'm trying to integrate this API wuth my PHP CodeIgniter framework. So I followed the documentation but I got stuck when I used curl

This is my source code below ..

require_once(APPPATH.'libraries/kraken-php-master/Kraken.php');
        $kraken = new Kraken("SOME_KEY", "SOME_SECRET");


        $params = array(
                "file" => base_url()."include/".$dataIn['logo'],
                "wait" => true
        );

        $dataj='{"auth":{"api_key": "SOME_KEY", "api_secret": "SOME_SECRET"},"file":'.base_url()."include/".$dataIn['logo'].',wait":true}';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://api.kraken.io/v1/upload");
        curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-Type: application/json')); 

        curl_setopt($ch, CURLOPT_POSTFIELDS, $dataj);
        $response = curl_exec($ch);
        curl_close($ch);

        $data = $kraken->upload($params);
        print_r($response);exit();

And I got this result

"{"success":false,"message":"Incoming request body does not contain a valid JSON object"}1"

So can anyone please help me,

And thanks in advance,

ponury-kostek
  • 7,824
  • 4
  • 23
  • 31
  • could you post a copy of the json you send out? after you have added the `base_url` and `$dataIn` – Mederic May 30 '17 at 08:59
  • {"auth":{"api_key":"38cd89fd7fc2b420","api_secret":"9d980cb857ee2d6779c1d"},"url":"http:\/\/localhost\/design-ninja\/include\/uploads\/Screenshot_from_2017-02-23_12-54-576.png","wait":true} – mousa.Alshaikh May 31 '17 at 03:17

2 Answers2

0

DONT POST YOUR API_KEY AND API_SECRET

The error message is quite clear, your json object is not valid. For instance this would be a valid JSON object for your request:

{
    "auth": {
        "api_key": "SOME",
        "api_secret": "SECRET"
    },
    "file": "somefile.txt",
    "wait": true
}

In your php code you are setting up a $params array but then you don't use it. Try this:

$dataj='{"auth":{"api_key": "SOME_KEY", "api_secret": "SOME_SECRET"},"file":"' . $params["file"]. '", "wait":true}';

You can validate your JSON HERE

lloiacono
  • 4,714
  • 2
  • 30
  • 46
0

You should use json_encode function to generate your JSON data

$dataj = json_encode([
    "auth" => [
        "api_key" => "API_KEY",
        "api_secret" => "API_SECRET"
    ],
    "file" => base_url() . "include/" . $dataIn['logo'],
    "wait" => true
]);

EDIT: Here is an example from https://kraken.io/docs/upload-url so you don't need to use curl

require_once("Kraken.php");

$kraken = new Kraken("your-api-key", "your-api-secret");

$params = array(
    "file" => "/path/to/image/file.jpg",
    "wait" => true
);

$data = $kraken->upload($params);

if ($data["success"]) {
    echo "Success. Optimized image URL: " . $data["kraked_url"];
} else {
    echo "Fail. Error message: " . $data["message"];
}
ponury-kostek
  • 7,824
  • 4
  • 23
  • 31