Search code examples
phplaravelcurlphp-curllaravel-api

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


Solution

  • 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));
    }