Good morning everyone. I need some help please with some API I am trying to connect to.
The client is using an old API so it's rather difficult to connect to their stuff.
I am not sure what the correct variation is to send Post Field data via the Laravel HTTP client. I have tried, get, post, etc. The hardcoded data in the cURL works, as soon as I change it to the variable it stops working.
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "api link here",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS =>'{
"objective_id": "1",
"industry_id": "1008",
"channel_id": "5",
"budget": "1",
"currency": "R",
"gender_id": "1",
"min_age": 18,
"max_age": 65,
"location_type":1,
"countries":"ZA",
"regions":"4,5,6",
"geo":"{[\"lat\":\"124\",\"lng\":\"4566\",\"rad\":5],[\"lat\":\"124\",\"lng\":\"4566\",\"rad\":5]}"
}',
CURLOPT_HTTPHEADER => [
"Authorization: Bearer " . config('token'),
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
\Log::info($channel_id);
if ($err) {
echo "cURL Error #:" . $err;
} else {
return json_decode($response);
}
If I try something like this, it does not work at all (An example, I know that data is missing here)
CURLOPT_POSTFIELDS =>'{
"industry_id": "'.$industry_id.'",
"objective_id": "'.$objective_id.'",
"location_type":1,
"countries":"ZA",
"regions":"377,428",
"geo":"[{\"latitude\":\"-32.2968402\",\"longitude\":\"26.4193890\",\"radius\":5, \"distance_unit\":\"kilometer\"},{\"latitude\":\"-26.2707593\",\"longitude\":\"28.1122679\",\"radius\":5, \"distance_unit\":\"kilometer\"}]",
"channel_id": "'.$channel_id.'",
"budget": "10000",
"budget_multiplier": "2",
"currency": "R",
"gender_id": "1",
"min_age": 18,
"max_age": 65
}',
Can someone help me convert the first on into a working Http version, I am not able to find info on post fields via the Laravel Documentation, I have also tried this (just an example - I know that there are variables missing here)
$response = Illuminate\Support\Facades\Http::retry(3, 100)->withToken(config('token'))->get('api link here', [
'industry_id' => $industry_id,
'product_id' => $product_id,
'touchpoint_id' => $touchpoint_id,
'goal_id' => $goal_id,
'channel_id' => $channel_id,
]);
if ($response->successful()) {
return json_decode($response);
}
if ($response->failed() || $response->clientError()) {
return redirect(route('serverUnreachable'));
}
Any help will be grateful as I am really at a loss here.