need php api help - php

hi friends i am trying to solve how i can access the halo reach api for get states bungie has released a api for this here is the link http://www.bungie.net/fanclub/statsapi/Group/Resources/Article.aspx?cid=545064
how can i access this service via php and display some stats need help thanks
i am trying like this
<?php
require_once('lib/nusoap.php');
$wsdl = "http://www.bungie.net/api/reach/ReachApiJson.svc?wsdl";
$client = new soapclient($wsdl, 'wsdl');
$parameters['parameters']['apikey'] = "xxx";
$result = $client->call("GetGameMetadata", $parameters);
?>

Consuming JSON in PHP is pretty simple.
<?PHP
$uri = 'http://www.bungie.net/api/reach/reachapijson.svc/game/metadata/xxx';
if (! $json = file_get_contents($uri)){ //cURL can be faster and more flexible, but this ought to work
trigger_error('API call failed!');
}
if (! $result = json_decode($json)){
trigger_error('API returned bad data?');
//maybe log some stuff here, so you can debug.
}
print_r($result); //see what you got.

Related

Returning value from restAPI in php, echo value not showing

I'm trying to return a value from my API using PHP, api can be found here:
code is as follows:
Im not seeing the echo on my page, don't see any errors and I believing im reading the json in correctly. Any help appreciated!
<?php
$titleid = 2;
$url = "http://kmoffett07.lampt.eeecs.qub.ac.uk/serverSide/buildapi.php?id={$titleid}";
$response = file_get_contents($url);
$returnvalue = json_decode($response, true);
echo $returnvalue["Age"];
?>
From what I can tell, the json is not valid on the server side (due to the "connected to db" text in front of the {} part). I think it would be a good idea to fix the server side response json data, if possible!
For now, here is a way to get the value it looks like you are intending to retrieve:
<?php
$titleid = 2;
$url = "http://kmoffett07.lampt.eeecs.qub.ac.uk/serverSide/buildapi.php?id={$titleid}";
$response = file_get_contents($url);
$adjusted_response = str_replace('connected to db', '', $response);
$returnvalue = json_decode($adjusted_response, true);
echo $returnvalue['tv_shows']['Age'];
?>
Output:
$ php example.php
16+
If the server side json data is fixed, I think you could shorten the code to something like this:
<?php
$titleid = 2;
$url = "http://kmoffett07.lampt.eeecs.qub.ac.uk/serverSide/buildapi.php?id={$titleid}";
$response = file_get_contents($url);
$returnvalue = json_decode($response, true);
echo $returnvalue['tv_shows']['Age'];
?>
The thing is that $response is returned as string , in order to fix that you need to edit your backend and make it give the response without "connected to db"

Can't get Customer Account Data API working in PHP

I'm trying to get Intuit CAD API to work from within a PHP application. I've read the docs using the recommended "SDK" (well, more like a demo app), search the web and tried multiple advice scattered here and there. Here's what I came up with:
// I also use log file for curl - which doesn't add any info
$curl_logfile = fopen('/tmp/curl_debug', 'w+');
$ch = curl_init();
$options = array();
$options[CURLOPT_VERBOSE] = true;
$options[CURLOPT_RETURNTRANSFER] = true;
$options[CURLOPT_TIMEOUT] = 360;
$options[CURLOPT_CERTINFO] = false;
$options[CURLOPT_SSL_VERIFYPEER] = false;
$options[CURLINFO_HEADER_OUT] = true;
$options[CURLOPT_STDERR] = $curl_logfile;
$options[CURLOPT_URL] = $signed_request['signed_url'];
curl_setopt_array($ch, $options);
$raw = curl_exec($ch);
if ($error_num = curl_errno($ch)) {
$error_desc = curl_error($ch);
}
// debug - get all info about the request just issues
$all_data = curl_getinfo($ch);
The code above return 400 as the return code (viewable using curl_getinfo()) and empty string in $raw.
How can I make my PHP app talk to Intuit CAD API?
Also, on the same note - some API method require transferring parameters, so it seem, as part of the path. For example: getInstitutionDetails (v1/institutions/INST-ID). Can I put those variables as parameters? Otherwise, I need to craft each request non-generically, according to the specific API method used :-(
Thanks!

extraction of tweets an storing

<?php
$mongo = new Mongo();
$db = $mongo->twitter;
$collection = $db->tweets;
$screen_name = "learnmongo";
$twitter = "http://api.twitter.com/1/statuses/user_timeline.json?";
$twitter .= "screen_name=" . $screen_name;
$curl = curl_init($twitter);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
$usertimeline = curl_exec($curl);
curl_close($curl);
$usertimeline = json_decode($usertimeline);
foreach ($usertimeline as $id => $item) {
$collection->insert($item);
}
?>
execution of this code gives error message as invalid parameters to foreach().plzz reply and hep me to solve this problem
You are using a deprecated verion of the Twitter API. Version 1.0 no longer works, you need to use version 1.1. You need to use version 1.1 of the call. The API URL has changed from:
http://api.twitter.com/1/statuses/user_timeline.json
to
http://api.twitter.com/1.1/statuses/user_timeline.json
Unfortunately the new 1.1 version requires authentication so your code will still not work until you implement an OAUTH solution for authentication.

RESTFUL web service response parsing issue

I am getting the restful web service response as well. But I am not able to parse it properly
my code looks like this
include 'RestClient.class.php';
error_reporting(E_ALL);
// Client Profile
$url = "http://localhost/lgen/index.php/api/client";
$ex = RestClient::get($url,array('requestType' =>'viewClientProfile',
'username' => 'uname',
'pass' =>'pass'));
echo $response = $ex->getResponse();
$xml = simplexml_load_string($response);
when print $response I am getting data on browser but while trying to parse it am not getting any kind of data
echo $response = $ex->getResponse();
You are creating and echoing a variable at the same time.
Try this:
$xml = simplexml_load_string($ex->getResponse());
var_dump($xml);

Get user's name from Facebook Graph API

I would like to know how is it possible to retrieve a string from an external page.
For example: In a PHP website, the user sends a facebook id, ex: 1157251270
And the website returns the name from http://graph.facebook.com/1157251270.
I hope I made it clear.
Thank you
The Graph API returns JSON strings, so you can use:
echo json_decode(file_get_contents('http://graph.facebook.com/1157251270'))->name;
or more verbose:
$pageContent = file_get_contents('http://graph.facebook.com/1157251270');
$parsedJson = json_decode($pageContent);
echo $parsedJson->name; // Romanos Fessas
See json_decode — Decodes a JSON string
If you are using Facebook's PHP SDK, you can also do this to query their graph API:
$fb = new Facebook();
$object = $fb->api('/1157251270');
you get it by:
$link = json_decode(file_get_contents('http://graph.facebook.com/1157251270'));
echo $link->name;
Nice tut:
http://webhole.net/2009/08/31/how-to-read-json-data-with-php/
Either you use :
$res_json = file_gets_contents("http://graph.facebook.com/1157251270")
$res = json_decode($res_json)
Or, if you prefer curl (here with https and access token) :
$ch4 = curl_init();
curl_setopt($ch4, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch4, CURLOPT_URL, "https://graph.facebook.com/1157251270?access_token=YOUR_ACCESS_TOKEN");
curl_setopt($ch4, CURLOPT_SSL_VERIFYPEER, false);
if(!$result = curl_exec($ch4))
{
echo curl_error($ch4);
} else {
$res = json_decode($res_json)
}
curl_close($ch4);
For facebook data you can use json_decode.
For another sites try with webscraping, for example:
here

Categories