我正在创建一个人们可以连接其Github帐户的网站。
我已将我的应用程序注册到Github上的开发人员应用程序中,以获得客户端ID和客户端密钥。
现在,我想用PHP中的cURL将人们重定向到Github的登录页面。 我在文档中找到的唯一一件事就是这个带有curl的命令行,但我不知道如何在PHP中使用它。
curl -u username https://api.github.com/user
感谢您的帮助!
-----------编辑-------------
好吧,我在Github上使用了关于Oauth的教程:http://www.phpgang.com/how-to-add-github-oauth-login-on-your-website-using-php_740.html
在授权之前一切正常。 然后我收到此错误
Notice: Trying to get property of non-object in C:\xampp\htdocs\codesourcing\github\index.php on line 19
Some error occured try again
第19行是下面的if($ gitResponce-> access_token):
$data = array('url' => 'https://github.com/login/oauth/access_token',
'data' => $postvars,
'header' => array("Content-Type: application/x-www-form-urlencoded","Accept: application/json"),
'method' => 'POST');
$gitResponce = json_decode(curlRequest($data));
if($gitResponce->access_token)
{
$data = array('url' => 'https://api.github.com/user?access_token='.$gitResponce->access_token,
'header' => array("Content-Type: application/x-www-form-urlencoded","User-Agent: ".appName,"Accept: application/json"),
'method' => 'GET');
$gitUser = json_decode(curlRequest($data));
echo '
<table>
<tr>
<td colspan="2"><a href="'.$gitUser->html_url.'" target="_blank"><img src="'.$gitUser->avatar_url.'" width="200px" height="200px"/></a></td>
</tr>
<tr>
<td>Name:</td>
<td>'.$gitUser->name.'</td>
</tr>
<tr>
<td>Email:</td>
<td>'.$gitUser->email.'</td>
</tr>
<tr>
<td>Location:</td>
<td>'.$gitUser->location.'</td>
</tr>
<tr>
<td>Website:</td>
<td>'.$gitUser->blog.'</td>
</tr>
</table>';
答案 0 :(得分:0)
您可以尝试curl php基本身份验证
<?php
$username='username';
$password='password';
$ch = curl_init();
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:7.0.1) Gecko/20100101 Firefox/7.0.1');
curl_setopt($ch, CURLOPT_URL, 'https://api.github.com/user');
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //get status code
$result=curl_exec ($ch);
curl_close ($ch);
print_r($status_code);
print_r($result);
我希望这会对你有所帮助。