在PHP中使用Curl
我正在做一个项目,帮助我通过在PHP中使用Curl,php,curl,Php,Curl,我正在做一个项目,帮助我通过PHP学习如何使用curl。我正在尝试使用我自己的帐户从测试中获取数据 我已通过以下方式成功通过我的域验证了我的帐户: https://api.twitch.tv/kraken/oauth2/authorize?response_type=code&client_id=...&redirect_uri=...&scope=user_read+channel_read+channel_subscriptions+user_subscription
PHP
学习如何使用curl
。我正在尝试使用我自己的帐户从测试中获取数据
我已通过以下方式成功通过我的域验证了我的帐户:
https://api.twitch.tv/kraken/oauth2/authorize?response_type=code&client_id=...&redirect_uri=...&scope=user_read+channel_read+channel_subscriptions+user_subscriptions+channel_check_subscription&state=...
我已经删除了客户端id
、重定向uri
和状态
,以显示我使用的链接
一旦成功通过身份验证,它将返回到我指定的域(redirect\u uri
),一旦返回到该域,网站只知道用户从twitch接受后生成的身份验证密钥
身份验证示例:3ofbaoidzkym72ntjua1Gmrr660nd
现在,我希望能够获得用户的用户名:
但我不确定如何解决这个问题,因为对我来说,似乎我已经完成了文档中所说的一切
我应该如何着手解决这个问题
编辑:
如果我要求使用我的:
我使用用户名的代码:
Array
(
[0] => Accept: application/vnd.twitchtv.v3+json
[1] => Authorization: OAuth code=scn89zerug002sr6r95z9ngbxmd0d2&scope=user_read+channel_read+channel_subscriptions+user_subscriptions+channel_check_subscription
)
但我还是得到了错误
编辑:
因此,我阅读了文档甚至更多,现在我了解到:
Array
(
[error] => Bad Request
[status] => 400
[message] => Parameter redirect_uri does not match registered URI
)
我甚至从twitch网站复制并粘贴到我的变量中,反过来,我仍然会得到相同的错误。。。现在,我甚至陷入了困境,但至少离我更近了一步。好的,我将在我这样做的时候与您一起做:d到目前为止,我已经能够获得一个用户,您出现错误的原因是您没有设置任何卷曲选项。我用它自学,我发现它在学习卷发时非常有用。代码本身是基本的,但很容易阅读。我设法理解它,并使它100%更复杂:D 首先,我将向您展示如何获得测试用户。 您要做的是设置选项,我将首先使用简单的方法 这两种方法是
CURLOPT_HEADER
和CURL_RETURNTRANSFER
。您可以使用init函数设置url
class twitch{
private$token,$twitch,$url="http://api.twitch.tv/kraken/";
protected$code,$state,$report;
private static$details;
public function __construct($code,$state){
$this->code=$code;
$this->state=$state;
self::$details=(object)array('client_id'=>'id','client_secret'=>'secret','return_url'=>'redirect');
$result=$this->makeCall('oauth2/token',true);
print_r($result);
}
protected function makeCall($extention,$auth=false,$object=true){
$this->twitch=curl_init($this->url.$extention);
//$opts=array(CURLOPT_)
if($auth!==false){
$opts=array(CURLOPT_FOLLOWLOCATION=>true,CURLOPT_RETURNTRANSFER=>true,CURLOPT_POST=>true,CURLOPT_POSTFIELDS=>json_encode(array('client_id'=>self::$details->client_id,'client_secret'=>self::$details->client_secret,'grant_type'=>'authorization_code','code'=>$this->code,'redirect_uri'=>self::$details->return_url)));
}else{
$opts=array(CURLOPT_HEADER=>array('Accept: application/cvd.twitchtv.v3+json','Authorization: OAuth '.$this->token),CURLOPT_RETURNTRANSFER=>true);
}
curl_setopt_array($this->twitch,$opts);
$result=curl_exec($this->twitch);
$this->report=array('info'=>curl_getinfo($this->twitch),'error'=>curl_error($this->twitch));
curl_close($this->twitch);
return($object===true)?json_decode($result):$result;
}
protected function userDetails(){
return$this->makeCall('user');
}
public function user(){
return$this->userDetails();
}
}
这将使您成为测试用户,并希望向您展示一点您做错了什么。如果要使用array方法,则必须使用curl选项作为数组键,以便set函数知道将什么设置为。(不要问我这一切在技术上是如何运作的:S)
我会更新,告诉你如何获得授权和数据,一旦我已经制定出来。但基本原则是您需要发送post数据并将CURLOPT_post
设置为true,并包括postdataCURLOPT_POSTFIELDS
,它必须是json数组,因为您的应用程序需要json,我相信
无论如何,阵列:
鉴于您已经知道如何授权用户,我将跳过这一部分,尽管我建议使用比$\u GET
更安全的工具。也许会话变量会更好一些
使用返回的身份验证获取特定用户。你想这样做:(对不起,我不能自己测试,我没有twitch开发帐户)
虽然我不知道你是如何得到oAuth令牌lol的,但这应该行得通
如果你想成为一名真正酷的程序员,我建议你为此学习一些课程。像这样
您需要
CURLOPT\u HTTPHEADER
来设置自定义授权:请求头。(likeamedCURLOPT_头
主要用于调试。)@mario,嘿!感谢您的评论,CURLOPT_HTTPHEADER
隐藏了调试,但仍然无法解决我的问题。但是谢谢!当我继续前进时,我会记住这一点:)我想你想要.urlencode($auth)代码>您仍然可以启用\u标题
。但大多数情况下,首先设置CURLOPT_VERBOSE
,看看实际发生了什么。()@Mihai,你好-我已经修改了$auth=$\u GET['code']
到$auth=urlencode($\u GET['code'])代码>。但我还是遇到了同样的问题。嘿,谢谢你的回复,虽然这似乎不是很有效,但仍然很有帮助。谢谢。@你好啊,好的,是的,这是一个有点奇怪的设置?您是否必须转到url才能获得响应?然后使用它在重定向url中提供的OAuth令牌?疯狂的您可能需要检查最后两个代码示例。我不确定auth是如何工作的,所以最后一个示例可能不起作用,但它会让您很好地了解如何构建以发送接收数据。如果你仍然没有看到回应。也许可以尝试使用curl\u getinfo()
和curl\u error()
来查看有哪些错误。@您好,看到了:p确保URL是100%准确的。包括http://etc.@hello ok我现在知道如何获得身份验证:)你想让我调整我的答案吗?:)当然尽管我不确定为什么重定向uri
与使用twitch保存的uri不同,尽管我复制并粘贴了它们。所以它们应该是一样的。一旦它停止发出错误,那么auth_令牌应该可以工作。
curl -H 'Accept: application/vnd.twitchtv.v3+json' \
-X GET https://api.twitch.tv/kraken/users/test_user1
<?php
$auth = urlencode($_GET['code']);
$twitch = curl_init();
$headers = array();
$headers[] = 'Accept: application/vnd.twitchtv.v3+json';
#$headers[] = 'Authorization: OAuth ' .$auth;
curl_setopt($twitch, CURLOPT_HTTPHEADER , $headers);
curl_setopt($twitch, CURLOPT_URL, "https://api.twitch.tv/kraken/users/...");
curl_exec($twitch);
?>
Array
(
[0] => Accept: application/vnd.twitchtv.v3+json
[1] => Authorization: OAuth code=scn89zerug002sr6r95z9ngbxmd0d2&scope=user_read+channel_read+channel_subscriptions+user_subscriptions+channel_check_subscription
)
class twitch {
var $base_url = "https://api.twitch.tv/kraken/";
var $client_id = "...";
var $client_secret = "...";
var $return_url = "...";
var $scope_array = array('user_read','channel_read','channel_subscriptions','user_subscriptions','channel_check_subscription');
public function get_access_token($code,$state) {
$ch = curl_init($this->base_url . "oauth2/token");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$fields = array(
'client_id' => $this->client_id,
'client_secret' => $this->client_secret,
'grant_type' => 'authorization_code',
'redirect_uri' => $this->redirect_url,
'code' => $code
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$data = curl_exec($ch);
$response = json_decode($data, true);
curl_close($ch);
echo "<pre>".print_r($this->redirect_url,true)."</pre>";
echo "<pre>".print_r($response,true)."</pre>";
return $response["access_token"];
}
};
$auth = new twitch();
print_r($auth->get_access_token($_GET['code'],$_GET['state']));
Array
(
[error] => Bad Request
[status] => 400
[message] => Parameter redirect_uri does not match registered URI
)
$twitch=curl_init('https://api.twitch.tv/kraken/users/test_user1');
curl_setopt($twitch,CURLOPT_HTTPHEADER,array('Accept: application/vnd.twitchtv.v3+json'));//must be an array.
curl_setopt($twitch,CURLOPT_RETURNTRANSFER,true);
$result=curl_exec($twitch);
$info=curl_getinfo($twitch);
print_r($result);
curl_set_opts($twitch,array(CURLOPT_HEADER=>array('Accept: application/vnd.twitchtv.v3+json',CURLOPT_RETURNTRANSFER=true));
$twitch=curl_init('https://api.twitch.tv/kraken/user');
curl_setopt($twitch,CURLOPT_HEADER,array('Accept: application/cvd.twitchtv.v3+json','Authorization: OAuth '.$_SESSION['token']));
curl_setopt($twitch,CURLOPT_RETURNTRANSFER,true);
$result=curl_exec($twitch);
print_r($result);
//don't forget to close!
curl_close($twitch);
$user=json_decode($result);
echo$user->display_name;
class twitch{
private$token,$twitch,$url="http://api.twitch.tv/kraken/";
protected$code,$state,$report;
private static$details;
public function __construct($code,$state){
$this->code=$code;
$this->state=$state;
self::$details=(object)array('client_id'=>'id','client_secret'=>'secret','return_url'=>'redirect');
$result=$this->makeCall('oauth2/token',true);
print_r($result);
}
protected function makeCall($extention,$auth=false,$object=true){
$this->twitch=curl_init($this->url.$extention);
//$opts=array(CURLOPT_)
if($auth!==false){
$opts=array(CURLOPT_FOLLOWLOCATION=>true,CURLOPT_RETURNTRANSFER=>true,CURLOPT_POST=>true,CURLOPT_POSTFIELDS=>json_encode(array('client_id'=>self::$details->client_id,'client_secret'=>self::$details->client_secret,'grant_type'=>'authorization_code','code'=>$this->code,'redirect_uri'=>self::$details->return_url)));
}else{
$opts=array(CURLOPT_HEADER=>array('Accept: application/cvd.twitchtv.v3+json','Authorization: OAuth '.$this->token),CURLOPT_RETURNTRANSFER=>true);
}
curl_setopt_array($this->twitch,$opts);
$result=curl_exec($this->twitch);
$this->report=array('info'=>curl_getinfo($this->twitch),'error'=>curl_error($this->twitch));
curl_close($this->twitch);
return($object===true)?json_decode($result):$result;
}
protected function userDetails(){
return$this->makeCall('user');
}
public function user(){
return$this->userDetails();
}
}