Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

LadyDeathKZN's avatar

PostFields in curl for Laravel HTTP

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.

0 likes
7 replies
LadyDeathKZN's avatar

Have also tried this: (example)

$data = [
        'industry_id' => $industry_id,
		...
    ];

    $response = Illuminate\Support\Facades\Http::retry(3, 100)->withToken(config('token'))->withBody(json_encode($data), 'application/json')->post('api link here');

    if ($response->successful()) {
        return json_decode($response);
    }

    if ($response->failed() || $response->clientError()) {
        return redirect(route('serverUnreachable'));
    }
Snapey's avatar

use ->post('url_here', $array_of_fields)

Snapey's avatar

first establish if you need get or post, you seem unsure

LadyDeathKZN's avatar

Hey Snapey, thanks for the above, I have also tried that and still get an error from the api. The API they setup uses both post or get, so it can be either. But have tried the get or post method and still no luck. the raw cURL works, but I need to send through variable data which it does not seem to pick up

Snapey's avatar

Keep it simple to start with

$data = [
    "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]}"
];

$url = 'http://your_api_url_here';

$response = Http::withHeaders([
        'Accept' => 'application/json',
        'Authorization' => 'Bearer ' . config('token')
    ])->post($url, $data);
LadyDeathKZN's avatar

@Snapey Unfortunately this still isn't working, I still get invalid data being sent. I will run a few tests this weekend to see what I can find.

Snapey's avatar

@LadyDeathKZN with curl you are sending a json encoded string, I converted it into a php array, but failed to spot that the geo element was a nested json string. Instead try

$data = [
    "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]
      ];

Please or to participate in this conversation.