Dhairya Lakhera
Dhairya Lakhera

Reputation: 4808

Get request working in guzzle but not in curl

I am working on an API where I have to get an eBay login page in response.

With guzzle my code is working fine and getting eBay login page Html in response but not if send a request with Curl. In the case of curl I am getting an empty string

$query = array(
    'client_id' => 'TestTest-Test-Test-12345678-12345678',
    'response_type' => 'code',
    'redirect_uri' => 'Test-redirect-uri',
    'scope' => 'https://api.ebay.com/oauth/api_scope/sell.item',
    'prompt' => 'login',
);

$link = "https://auth.sandbox.ebay.com/oauth2/authorize?".http_build_query($query);

My curl request

$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, $link);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); 
$output = curl_exec($handle);
curl_close($handle);
echo $output;

My Guzzle request

try {
    $response = $client->request('GET', $link);
    echo $response->getBody()->getContents();
} catch (\Exception $e) {
    dd($e);
}

I have tried lots of solution for curl but i am unable to figure it out why i am getting an empty response in Curl.

Upvotes: 1

Views: 213

Answers (1)

Syscall
Syscall

Reputation: 19780

The response probably contains a redirection.

You can add the CURLOPT_FOLLOWLOCATION option, to allow cURL to follow the Location: header.

$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, $link);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true); // Allows to follow redirections
$output = curl_exec($handle);
curl_close($handle);

Upvotes: 2

Related Questions