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

sandyboy's avatar

Laravel 4 CURL update request

Hi, I'm thinking is it possible to update the data when sending the POST request in Laravel? Currently, I already completed the Add function which is store data into the server using POST request. However, the server will return me duplicate ID if data is exits so how could I update the data based on ID? My function flow is like, user click one button then the data will start to post to the server if data no exits. At the same time, the system will update the data if the same ID is found.

This is my code that posting the data.

Route Route::post('product', 'ProductController@store');

controller

public function get_url()
{

    $url = 'https://api.com/product';

    return $url;
}

public function post_data($url, $data, $sync_date)
{
        $curl = curl_init();
        Debugbar::info($url);
        curl_setopt_array($curl, array(
            CURLOPT_URL => $url,
            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($data),
            CURLOPT_HTTPHEADER => array(
                // Set here requred headers
                "Api-key: " . Input::get('api_key'),
                "accept: */*",
                "accept-language: en-US,en;q=0.8",
                "content-type: application/json",
            ),
        ));
        
        $response = curl_exec($curl);

        $err = curl_error($curl);
        curl_close($curl);
        
}

    public function sync_product()
{
    $url = $this->get_url();
    
    
    $sync_data = Product::whereNull('synced_date')->get();

    foreach ($sync_data as $sync_date) {
        //processing data

            $this->post_data($url, $data, $sync_date);
     }//end of loop
    
     return View::make('sync_api.home');
}
0 likes
1 reply

Please or to participate in this conversation.