2

I'm having a hard time trying to send POST data to the twitch api (https://api.twitch.com/kraken/oauth2/token) and get back the json code... This is my current code:

$this->config->load("config", true);
$url = "https://api.twitch.tv/kraken/oauth2/token";
$postvars = "client_id=" . $this->config->item('twitch_clientid', 'config') . "&client_secret=" . $this->config->item('twitch_secret', 'config') . "&grant_type=user_read&redirect_uri=" . $this->config->item('twitch_redirect', 'config') . "&code=" . $key;

$temp = curl_init($url);
curl_setopt($temp, CURLOPT_POST, 1);
curl_setopt($temp, CURLOPT_POSTFIELDS, $postvars);
curl_setopt($temp, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($temp, CURLOPT_HEADER, 0);
curl_setopt($temp, CURLOPT_RETURNTRANSFER, 1);
$result = json_decode(@file_get_contents(curl_exec($temp)), true);

But... obviously that is not the way I should be doing it. First time working with curl.
Error:

A PHP Error was encountered 
Severity: Warning
Message: file_get_contents(): Filename cannot be empty
Filename: controllers/user.php
Line Number: 37

Result expected:

{
  "access_token": "[user access token]",
  "scope":[array of requested scopes]
}
1
  • What error(s) do you get? What is the desired and actual outcome? Commented Dec 31, 2013 at 13:45

1 Answer 1

1

The error is in this line:

$result = json_decode(@file_get_contents(curl_exec($temp)), true);

curl_exec returns false or data when returntransfer is on (https://www.php.net/curl_exec) therefore the code should look more like

if (! $data = curl_exec($ch)) { 
    //ERROR 
} else {
    $result = json_decode($data, true);
}
curl_close($ch); 

You can always set option VERBOSE to true, to have a better view of what is send to the server.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.