0

我最近使用 kraken.io API,我正在尝试将此 API 与我的 PHP CodeIgniter 框架集成。所以我遵循了文档,但是当我使用时我被卡住了curl

这是我下面的源代码..

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

我得到了这个结果

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

所以任何人都可以帮助我,

并提前感谢,

4

2 回答 2

0

您应该使用json_encode函数来生成您的 JSON 数据

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

编辑:这是来自https://kraken.io/docs/upload-url 的示例,因此您无需使用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"];
}
于 2017-05-30T08:46:57.917 回答
0

不要发布您的 API_KEY 和 API_SECRET

错误信息很清楚,您的 json 对象无效。例如,这将是您请求的有效 JSON 对象:

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

在您的 php 代码中,您正在设置一个 $params 数组,但随后您不使用它。尝试这个:

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

您可以在此处验证您的JSON

于 2017-05-30T08:37:05.053 回答