isethi
isethi

Reputation: 755

Curl and php wont work if loaded through browser

I am trying to write the response of this call to a file but it will only write if I run it in the cli. When I run it through the browser nothing happens.

$url = 'https://api.dropbox.com/1/oauth2/token';

$app_key = '************';
$app_secret = '***********';

$auth_code = '******************************';
$redirect_uri = "https://dev.subely.com/test/";

$rdata = 'code=' . $auth_code . '&grant_type=authorization_code&redirect_uri=' . $redirect_uri;

$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $rdata);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch, CURLOPT_HEADER, 0);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt( $ch, CURLOPT_USERPWD, $app_key.':'.$app_secret);


$response = curl_exec( $ch );

file_put_contents("/tmp/response.log", $response, FILE_APPEND);
echo "done";
?>

Upvotes: 1

Views: 79

Answers (1)

Naincy
Naincy

Reputation: 2943

Most probably it is the SSL verification problem (default SSL verfication is TRUE). As you said its working in CLI but from web it's not working.

Add

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

Also, it will good if you check curl execution

$response = curl_exec( $ch );

if($response === false) {
    echo 'Curl error: ' . curl_error($ch);
} else {
   file_put_contents("/tmp/response.log", $response, FILE_APPEND);
}

Upvotes: 2

Related Questions