PHP: Using file_get_contents to access an API - Empty Response
Get the solution ↓↓↓Sorry for the messy code in advance. I want to write a code which returns me infos from the official Blizzard API, which I can then print out on my homepage. The code doesn't throw any errors but it doesn't print out something either. For Starters:
I would also prefer using CURL, but my homepage is on a Wordpress Hosting Site and I don't know how to install the CURL Library that way
allow_furl_open is on
$url = "https://eu.battle.net/oauth/token"; $data = array('grant_type' => 'client_credentials'); //HTTP options $opts = array('http' => array( 'method' => 'POST', 'header' => array ('Content-type: multipart/form-data', 'Authorization: Basic ' . base64_encode("$client_id:$client_pass")), 'content' => json_encode($data) ) ); //Do request $context = stream_context_create($opts); $json = file_get_contents($url, false, $context); $result = json_decode($json, true); $accessToken = $json['access_token']; $tokenURL = "https://us.api.blizzard.com/data/wow/token/?namespace=dynamic-eu"; $opts2 = array('http' => array( 'method' => 'POST', 'header' => array('Content-type: multipart/form-data', 'Authorization: Bearer ' . $accessToken), ) ); $context2 = stream_context_create($opts2); $json2 = file_get_contents($tokenURL,false,$context2); $result2 = json_decode($json2, true); $tokenprice = $result2['price']; echo "<p>Tokenpreis:" .$tokenprice. "</p>";
I didn't add the $client_id and $client_pass into the code snippet, but this exists obviously. I used this PHP CURL Snippet as template. And this is a short explanation on blizzard's site on how is this supposed to work:
Anyone got any ideas what went wrong? I am really out of ideas here and would love anyone who could help.
Thanks in advance
Answer
Solution:
Based on the curl example from the linked API docs, the content type should beapplication/x-www-form-urlencoded
, and so for the initial request to https://eu.battle.net/oauth/token you should be able to follow the example here.
In your case this will look something like:
$url = 'https://eu.battle.net/oauth/token';
$data = array('grant_type' => 'client_credentials');
$opts = array(
'http' => array(
'method' => 'POST',
'header' => array (
'Content-type: application/x-www-form-urlencoded',
'Authorization: Basic ' . base64_encode("$client_id:$client_pass")
),
'content' => http_build_query($data)
)
);
$context = stream_context_create($opts);
$json = file_get_contents($url, false, $context);
Also, in your code you are storing the raw result from the initial request in$json
, then the decoded array in$result
, but attempting to get theaccess_token
from the initial request, instead of the decoded array.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: ftp_put(): can't open that file: no such file or directory
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.