Reputation: 353
I am working on an API from RIOT games. I use this code to in order to let it work:
<?php
header('Content-Type: text/html; charset=utf-8');
ini_set("display_errors", "1"); error_reporting(E_ALL);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://euw.api.pvp.net/api/lol/euw/v1.4/summoner/by-name/Electronic Arts, Legendary, Kiddo?api_key=');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);
$json = json_decode($response, true);
//var_dump($json);
//$json = array_pop($json);
echo $json['id'] . ',' . $json['name'] . ',' . $json['summonerLevel'] . "<br/>";
?>
When I directly paste the api call url in my browser this will return:
{
"kiddo": {
"id": 35037868,
"name": "kiddo",
"profileIconId": 505,
"summonerLevel": 30,
"revisionDate": 1412534473000
},
"legendary": {
"id": 229888,
"name": "Legendary",
"profileIconId": 592,
"summonerLevel": 30,
"revisionDate": 1412259698000
},
"electronicarts": {
"id": 31827832,
"name": "Electronic Arts",
"profileIconId": 575,
"summonerLevel": 30,
"revisionDate": 1412541482000
}
}
But when I execute the first code block, the var_dump($json)
only returns NULL
I think there must be a simple mistake which I am blind for, could anyone help me out?
Upvotes: 1
Views: 2710
Reputation: 3972
this is to do with spaces in your url. you need to put %20
/api/lol/euw/v1.4/summoner/by-name/Electronic%20Arts,%20Legendary,%20Kiddo?api_key=
complete example
$url = str_replace(' ', '%20', 'https://euw.api.pvp.net/api/lol/euw/v1.4/summoner/by-name/Electronic Arts, Legendary, Kiddo?api_key=');
$ch = curl_init($url);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // do not set to false, vunerable to man in the middle attacks
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$resp = curl_exec($ch);
$errno = curl_errno($ch);
$errmsg = curl_error($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
var_dump($errno, $errmsg, $httpcode, $resp);
Upvotes: 2
Reputation: 175
Paste url in Browser, check if that works. Browser encodes url and then fetches result,
Use the same encoded url in your code, that would work.
Upvotes: 0