简体   繁体   中英

laravel php curl request for finerworks api

I want to use finerworks api and my curl code is here

$data1 = array(
'web_api_key' => '********-****-****-*****-************',
'app_key' => '********-****-****-*****-************',
);
$data2['credentials'] = $data1;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.finerworks.com/v3/test_my_credentials",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30000,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode($data2),
CURLOPT_HTTPHEADER => array(
    "accept: */*",
    "accept-language: en-US,en;q=0.8",
    "content-type: application/json",
),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
echo "cURL Error #:" . $err;
} else {
print_r(json_decode($response));
}

when i run this code here is my request response below

stdClass Object ( [Message] => The request is invalid. [ModelState] => stdClass Object ( [ApiC.Models.authorization_credentials] => Array ( [0] => Missing or unauthorized api credentials ) ) ) 

Please help me to solve this thanks in advance

Pass the credentials in header

$data1 = array(
    'web_api_key' => '********-****-****-*****-************',
    'app_key' => '********-****-****-*****-************',
);
$data2['credentials'] = $data1;
$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_URL => "https://api.finerworks.com/v3/test_my_credentials",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30000,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => json_encode($data2),
    CURLOPT_HTTPHEADER => array(
        "accept: */*",
        "accept-language: en-US,en;q=0.8",
        "content-type: application/json",
        "web_api_key: YOUR web_api_key",
        "app_key: YOUR app_key",
    ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
    echo "cURL Error #:".$err;
} else {
    print_r(json_decode($response));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM