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

Aq1's avatar
Level 1

curl : Failed to store data in DB

Using curl in my project first time. Trying to generate curl request to register user but data is not stored in my database.

The code to make curl request after form submission:

web.php file

Route::post('/submit-form', 'Auth\RegisterController@postRequest')->name('submit-form');

public function postRequest(){
        $data = ['Name'=>'abc', 'Email'=>'[email protected]'];
        $url = 'http://localhost/myproject/public/index.php/postReqFromView';
        try{
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL,$url);
            curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            $server_output = curl_exec($ch);
            curl_close ($ch);
            print_r($server_output);
        }
        catch (\Exception $exp){
            print_r($exp->getMessage());
        }
    }

The following controller is in my other project where I am trying to receive the request and process it

api.php file

Route::post('/postReqFromView', 'studentRegisterController@postRequest');

my controller

  public function postRequest(Request $request){
       $abc = DB::table('tbl_users')->insert(['username' => $request->Name, 'email' => $request->Email]);
        return $abc ;
    }

Please help me out

0 likes
4 replies
Snapey's avatar

are you using php artisan serve?

Aq1's avatar
Level 1

not in this project

s.spaan's avatar

Any routes defined in the api.php are prefixed with /api.

So i think your url should be (if http://localhost/myproject/public/index.php is actually working):

$url = 'http://localhost/myproject/public/index.php/api/postReqFromView';

But it's better to serve the project like @snapey suggested and use a URL like

$url = 'http://localhost:8080/api/postReqFromView';
Snapey's avatar

I was not suggesting that php artisan serve is used

I was checking that it is not since it is single threaded and cannot handle making a curl call to another route on the same web server

You have a client and server here. You should work out how you can test one or the other on their own.

You also need to help by saying what is happening. You imply nothing is saved but this could be for any number of reasons.

Please or to participate in this conversation.