PHP example
Yussef.2074:
It’s been a while since I’ve done any programming in php. What would a simple php request (that then puts the request into a string) for say the events.json look like?
How about a java servlet version of that?
TimeBomb.3427:
Haven’t tested this code, but something like this should work for PHP (curl is required):
// create a new cURL resource
$ch = curl_init();// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, “https://api.guildwars2.com/v1/events.json?world_id=1001”);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // so curl_exec returns data// grab URL and pass it to the browser; store returned data
$curlRes = curl_exec($ch);// close cURL resource, and free up system resources
curl_close($ch);// Decode returned JSON so it can be handled as a multi-dimensional associative array
$data = json_decode($curlRes, true);
vigorator.7420:
That code should work, i’m using something almost exactly like that for my stuff.
Ravenmoon.5318:
If you find cURL too hard to understand at first you can easily get away with file_get_contents for GET requests
e.g.
$data = json_decode( file_get_contents($url) );
smiley.1438:
file_get_contents() needs allow_url_fopen set to true, so it’s no option to rely on since a lot providers don’t allow it for security reasons.
If you want your script to run on any server, try something like this:
https://gist.github.com/codemasher/4d30a47df24195ac509f
function gw2_api_request($request){
$url = parse_url('https://api.guildwars2.com/v1/'.$request);
if(!$fp = @fsockopen('ssl://'.$url['host'], 443, $errno, $errstr, 5)){
return 'connection error: '.$errno.', '.$errstr;
}
$nl = "\r\n";
$header = 'GET '.$url['path'].'?'.$url['query'].' HTTP/1.1'.$nl.'Host: '.$url['host'].$nl.'Connection: Close'.$nl.$nl;
fwrite($fp, $header);
stream_set_timeout($fp, 5);
//in case of a chunked response, try http://stackoverflow.com/a/3290822
$response = '';
do{
if(strlen($in = fread($fp,1024))==0){
break;
}
$response.= $in;
}
while(true);
$response = explode($nl,$response);
if(isset($response[0]) && $response[0] == 'HTTP/1.1 200 OK'){
$response = json_decode($response[count($response)-1]);
}
return $response;
}
JonnyTen.9428:
Here’s something extremely simple for the curious PHP beginners that simply want a quick result. Wouldn’t recommend building on this method though:
function getItem($id) {
$response = file_get_contents(“https://api.guildwars2.com/v1/item_details.json?item_id=”.$id);
$response = json_decode($response, false);
return $response;
}
//Use as following:
echo getItem(12345)->name;
Alternatively I use the HttpRequest class
function getItem($id)
{
$req = new HttpRequest(‘https://api.guildwars2.com/v1/item_details.json’, HttpRequest::METH_GET);
//Add GET values, in this case just the item_id and language
$req->addQueryData(array(
‘item_id’ => $id,
‘lang’ => ‘en’
));
try {
//Sends it and fills $result Object
$result = $req->send();
//If any errors
} catch (HttpException $ex) {
echo $ex;
}
return ($result);
}
print_r(getItem(12345));
smiley.1438:
protip: try the [pre] tag to post code
What i’ve written just one post before applies also to your first code example.