Sono nuovo di php, ma mi piace già molto...
Per ora per fare delle prove lo sto usando su un dominio windows e sto provando a dialogare con google... però c'è una funzione curl che non funziona perchè pare che non ci sia nel pacchetto dell'hosting. si può sostituire tale funzione con qualcos'altro?
se serve il codice che sto manipolando è questo:
Codice PHP:
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
//$client_id, $client_secret, $demo_redirect_uri, $demo_scope defined in this file
// make inc/vars.inc not browsable on the server
//$path .= "/inc/vars.inc";
$path .= "inc/vars.inc";
include_once($path);
//so session vars can be used
session_start();
//Oauth 2.0: exchange token for session token so multiple calls can be made to api
if(isset($_REQUEST['code'])){
$_SESSION['accessToken'] = get_oauth2_token($_REQUEST['code']);
}
//returns session token for calls to API using oauth 2.0
function get_oauth2_token($code) {
global $client_id;
global $client_secret;
global $redirect_uri;
$oauth2token_url = "https://accounts.google.com/o/oauth2/token";
$clienttoken_post = array(
"code" => $code,
"client_id" => $client_id,
"client_secret" => $client_secret,
"redirect_uri" => $redirect_uri,
"grant_type" => "authorization_code"
);
$curl = curl_init($oauth2token_url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $clienttoken_post);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$json_response = curl_exec($curl);
curl_close($curl);
$authObj = json_decode($json_response);
if (isset($authObj->refresh_token)){
global $refreshToken;
$refreshToken = $authObj->refresh_token;
}
$accessToken = $authObj->access_token;
return $accessToken;
}
//calls api and gets the data
function call_api($accessToken,$url){
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$curlheader[0] = "Authorization: Bearer " . $accessToken;
curl_setopt($curl, CURLOPT_HTTPHEADER, $curlheader);
$json_response = curl_exec($curl);
curl_close($curl);
$responseObj = json_decode($json_response);
return $responseObj;
}
$loginUrl = sprintf("https://accounts.google.com/o/oauth2/auth?scope=%s&state=%s&redirect_uri=%s&response_type=code&client_id=%s",$demo_scope,$state,$redirect_uri,$client_id);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>OAuth 2.0 Demo Page</title>
<link rel="stylesheet" type="text/css" href="css/960.css" media="screen" />
<link rel="stylesheet" type="text/css" href="css/style.css" media="screen" />
</head>
<body>
<div id="wrapper">
<div id="header" class="container_16">
<h1><span>Google API</span> with OAuth 2 <span>PHP</span></h1>
</div>
<div id="content-wrap" class="container_16">
<div class="grid_8 prefix_4 suffix_4">
<h2>Authorize</h2>
[url="<?php echo $loginUrl ?>"]Grant access with Google account for basic user info[/url]</p>
web3DRendering will return the name on your Google account. It does not ask for offline access.</p>
You can revoke access by:</p>
[list=1][*]going to your Google Account Settings (Impostazioni account)[*]click "Edit" ("Modifica") next to Authorizing applications & sites (Autorizzazione di applicazioni e siti)[*]click "Revoke Access" next to the app[/list]
</p>
<?php
if (isset($_SESSION['accessToken'])){
$accountObj = call_api($_SESSION['accessToken'],"https://www.googleapis.com/oauth2/v1/userinfo");
$your_name = $accountObj->name;
echo "<p class='successMessage'>The name on your Google account is: " . $your_name . "</p>";
session_unset();
}
if(isset($_REQUEST['error'])){
echo "<p class='errorMessage'>Error: " . $_REQUEST['error'] . "</p>";
}
?>
</div>
</div>
</div>
</body>
</html>
grazie ragazzi