简体   繁体   中英

Laravel 5.4 Eloquent and curl

Is it posible that an Eloquent query can be affected by a request made with curl?

Here is what i mean, I have this controller method that returns an user's info

class TestController extends Controller
{
    public function test(Request $request)
    {
        $User= User::where('email','user@mail.com')->first();
        return response($User);
}

So when i go to the browser and request that method, it returns the user's info as expected. Now when i send the request using curl, it returns NULL.

Here is my curl:

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
    curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);

    $result['content'] = curl_exec($ch);

Now here comes the really weird part, if i change this query:

$User= User::where('email','user@mail.com')->first();

to this

$User= User::first();

and send the request with curl again, it works.

It looks like somehow the Eloquent behaviour is affected when using curl, I don't understand why

(By the way the users table contains only 1 user)

Update I have found something even more strange, when using curl this part $User=User::first() is actually getting the user from the client db (i have the client app and the server app in the same machine but with different urls) not from the db that this api is using, although both applications have the correct db in their corresponding .env file. I even set the namespace to App/Api/User in the api and App/User in the client to avoid any namespace conflict, but the problem persists

I believe this is because you are not setting content type in your curl request, please try the following curl

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json',
    'Connection: Keep-Alive'
]);

$result['content'] = curl_exec($ch);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM