laravel php curl request for finerworks api - php

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

Related

GBG Id scan Api in php

I need to implement GBG id scan api to recognize uploaded identity documents verification.
I have tried as following. Platform is php codeigniter.
$img_path = base_url()."images/NID-1.jpg";
$img_content = file_get_contents($img_path);
$encode_img = base64_encode($img_content);
$img_path2 = base_url()."images/NID-2.jpg";
$img_content2 = file_get_contents($img_path2);
$encode_img2 = base64_encode($img_content2);
$ids_user_name = "XXXXXXXXXX";
$ids_passwd = "XXXXXXXXXXX";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://poc.idscan.cloud/idscanenterprisesvc/token',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => 'username='.$ids_user_name.'&password='.$ids_passwd.'&grant_type=password&area=scanning',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/x-www-form-urlencoded'
),
));
$response = curl_exec($curl);
curl_close($curl);
$decode_response = json_decode($response);
$access_token = $decode_response->access_token;
$token_type = $decode_response->token_type;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://poc.idscan.cloud/idscanenterprisesvc/journey/upload',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>"{'InputImages': [{'Name':'WhiteImage','ImageFormat':'jpg','Data':'".$encode_img."'}],'IsDocumentExtracted':'false','Source':2}",
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer '.$access_token,
'Content-Type: application/json'
),
));
$response1 = curl_exec($curl);
curl_close($curl);
$decode_response1 = json_decode($response1);
echo '<pre>';
print_r($decode_response1);
exit;
Response:
stdClass Object
(
[PersonEntryId] => 00000000-0000-0000-0000-000000000000
[EntryDateTime] => 0001-01-01T00:00:00
[CurrentResult] => ProcessingError
[IsFinished] =>
[RequestId] => 00000000-0000-0000-0000-000000000000
[HasError] =>
[EntryData] => stdClass Object(
)
)
pls Help me to solve this ProcessingError
i have referred this document https://docs.idscan.com/docs/ieos-web-api-8-7/actions/upload/
I need to upload identity document to IDscan backend to be processed along with an optional face match input data to match two faces.
Thanks in advance..

Place data in a URL encoded from a form post action

I have a form that get the data values (uid,f_1_email, etc...) i need to send to an API, i need to get those values a put in this URL encoded.
"cid=199&sid=2&uid=[uid]&f_1_email=[f_1_email]&f_3_firstname=[f_3_firstname]&f_4_lastname=[f_4_lastname]&f_11_postcode=[f_11_postcode]&f_12_phone1=[f_12_phone1]&f_135_nombre_empresa=[f_135_nombre_empresa]&f_134_cantidad_vehiculos=[f_134_cantidad_vehiculos]&f_133_tipo_servicio=[f_133_tipo_servicio]
"
This is the full code
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://leadtowin.databowl.com/api/v1/lead",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "cid=199&sid=2&uid=[uid]&optin_email=[optin_email]&optin_email_timestamp=[optin_email_timestamp]&optin_phone=[optin_phone]&optin_phone_timestamp=[optin_phone_timestamp]&optin_sms=[optin_sms]&optin_sms_timestamp=[optin_sms_timestamp]&optin_postal=[optin_postal]&optin_postal_timestamp=[optin_postal_timestamp]&f_1_email=[f_1_email]&f_3_firstname=[f_3_firstname]&f_4_lastname=[f_4_lastname]&f_11_postcode=[f_11_postcode]&f_12_phone1=[f_12_phone1]&f_135_nombre_empresa=[f_135_nombre_empresa]&f_134_cantidad_vehiculos=[f_134_cantidad_vehiculos]&f_133_tipo_servicio=[f_133_tipo_servicio]
",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/x-www-form-urlencoded",
"Accept-Encoding: UTF-8",
"Content-Type: application/x-www-form-urlencoded",
"Cookie: PHPSESSID=d053646c7953d9116849a8aa4717ab81"
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
I try this but it does not work
"cid=199&uid=$_POST['uid']&f_1_email=$_POST['email']&f_3_firstname=$_POST['firstname']&f_4_lastname=$_POST['lastname']&f_11_postcode=$_POST['meta_Zip']&f_12_phone1=$_POST['mobile_phone']&f_135_nombre_empresa=$_POST['meta_empresa']&f_134_cantidad_vehiculos=$_POST['meta_cantidadVehiculos']&f_133_tipo_servicio=$_POST['meta_Gestion']"
Maybe the native PHP functionality can easy this process for you ( https://www.php.net/manual/en/function.http-build-query.php ). Using an array also makes it a lot better readable, and thus cleaner and less error phrone:
<?php
$post_array = array(
"cid" => "199",
"sid" => "2",
"uid" => $_POST['uid'],
"optin_email" => $_POST['optin_email'],
"optin_email_timestamp" => $_POST['optin_email_timestamp'],
"optin_phone" => $_POST['optin_phone'],
"optin_phone_timestamp" => $_POST['optin_phone_timestamp'],
"optin_sms" => $_POST['optin_sms'],
"optin_sms_timestamp" => $_POST['optin_sms_timestamp'],
"optin_postal" => $_POST['optin_postal'],
"optin_postal_timestamp" => $_POST['optin_postal_timestamp'],
"f_1_email" => $_POST['f_1_email'],
"f_3_firstname" => $_POST['f_3_firstname'],
"f_4_lastname" => $_POST['f_4_lastname'],
"f_11_postcode" => $_POST['f_11_postcode'],
"f_12_phone1" => $_POST['f_12_phone1'],
"f_135_nombre_empresa" => $_POST['f_135_nombre_empresa'],
"f_134_cantidad_vehiculos" => $_POST['f_134_cantidad_vehiculos'],
"f_133_tipo_servicio" => $_POST['f_133_tipo_servicio']
);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://leadtowin.databowl.com/api/v1/lead",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => http_build_query($post_array),
CURLOPT_HTTPHEADER => array(
"Content-Type: application/x-www-form-urlencoded",
"Accept-Encoding: UTF-8",
"Content-Type: application/x-www-form-urlencoded",
"Cookie: PHPSESSID=d053646c7953d9116849a8aa4717ab81"
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
?>
This should do.

Curl Response takes very long

my Curl Response takes very long. Is there any way to make it faster?
This is an api for checking the status from elevators.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.deutschebahn.com/fasta/v2/facilities?type=ELEVATOR&stationnumber=1401",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json",
"authorization: Bearer XXXXXXXXXXX",
"cache-control: no-cache"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
$result = json_decode($response, true);

PHP cURL not return a response, POSTMAN returns response

I am running a cURL request that looks like this,
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json",
"Authorization: Bearer " . $token
)
));
$response = curl_exec($curl);
die(var_dump($response));
Would there be any reason for the my cURL in PHP not returning a response?

How to send array and object with curl

I am a beginner with curl. I want to make a request where I post different data.
I have a code
$curl = curl_init();
$fields = (object) array(
'contactFilter' => (object) array(
'clicked_message.messageid' => '5',
'messages_sent.messageid' => '5'
),
'exportAttributes' => 'email',
);
$fields = json_encode($fields);
$fields_string = http_build_query($fields);
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.sendinblue.com/v3/contacts/export",
CURLOPT_HTTPHEADER => array(
'Accept: application/json',
'Content-Type: application/json',
'api-key: my-key-12345',
),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $fields_string
));
$response = curl_exec($curl);
Documentation says that I need;
contactFilter object (X= campaign id): {"clicked_message.messageid": X,"messages_sent.messageid": X}
and
exportAttributes` array of strings For example, `['fname', 'lname, 'email'].
how should my request should look like?
You need to use CURLOPT_POSTFIELDS to post the fields, for example
$fields = array(
'username' => "annonymous",
'api_key' => "1234"
);
$fields_string = http_build_query($fields);
Now use variable fields_string to send array data
curl_setopt($ch, CURLOPT_POSTFIELDS,$fields_string);
Your curl request will be look like
curl_setopt_array($curl,
array(
CURLOPT_URL => "https://api.sendinblue.com/v3/contacts/export",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $fields_string
)
);
Updated Code-
$curl = curl_init();
$fields = (object) array(
'contactFilter' => (object) array(
'clicked_message.messageid' => '5',
'messages_sent.messageid' => '5'
),
'exportAttributes' => 'email',
);
$fields = json_encode($fields);
//$fields_string = http_build_query($fields);
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.sendinblue.com/v3/contacts/export",
CURLOPT_HTTPHEADER => array(
'Accept: application/json',
'Content-Type: application/json',
'api-key: my-key-12345',
),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $fields
));
$response = curl_exec($curl);
echo $response;
print_r($response);

Categories