<?php
function fetchUrl($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$feedData = curl_exec($ch);
curl_close($ch);
return $feedData;
}
$profile_id = "xxxxxxxxxxxxx";
//App Info, needed for Auth
$app_id = "xxxxxxxxxxxx";
$app_secret = "xxxxxxxxxxx";
$authToken = fetchUrl("https://graph.facebook.com/oauth/access_token?type=client_cred&client_id={$app_id}&client_secret={$app_secret}");
$json_object = fetchUrl("https://graph.facebook.com/{$profile_id}/posts?{$authToken}");
$feedarray = json_decode($json_object);
echo $json_object;
foreach ( $feedarray->data as $feed_data )
{
echo "<h2>{$feed_data->name}</h2><br />";
echo "{$feed_data->message}<br /><br />";
}
?>
When I try to run my code with my details (fully open page), I get an output of
Warning: Invalid argument supplied for foreach() in /home/xxxxxxxxx/public_html/includes/footer.php on line 52
When I use the code below it prints bool(false).
$feedData = curl_exec($ch);
var_dump($feedData); die();
I have worked out that the fetchUrl's are not working and I have tried many different others but none seem to work...
Any ideas?
Related
I am having this demo link to get acces_token for the user verifications of the user login.
http://testapp.test/app.php/v1/verifyUser?username=admin&password=password&grant_type=password&client_id=6_20jeacfmdv0gwgg4kcgw44swwkoo8ogog8g4gwggwo0wko08ow&client_secret=38311bdlv884wc0ws4g048k0ieei8oc4oc0cmswok8w33o0s9e
After sending request using postman i am getting the access token in the response
<?xml version="1.0"?>
<user_info>
<access_token>NGI20QFmNDE1HAUjYWEyMjVlbsryNWRjNGJmOGI5ZDNiLhA1OTI5MjkzZjIwZmE4YmYxOHYwMTQ3NTUzYTdmMg</access_token>
<expires_in>86400</expires_in>
<token_type>bearer</token_type>
<scope/>
<refresh_token>AWMxYmVhYmFkNDJkOTVjYjM1YWQwNTVmOTcwYmIyKaR98TE1Y2E5KLZkZTY0ZjA3Mjc3MzQzOeGmTTUzODlmYw</refresh_token>
<user_id>007</user_id>
<username>admin</username>
<email>email#admin.com</email>
<firstname>admin</firstname>
<lastname>admin</lastname>
<ip_address>xxx.xxx.xx.xxx</ip_address>
<last_login>
<date>2019-07-12 18:21:27.000000</date>
<timezone_type>4</timezone_type>
<timezone>Central</timezone>
</last_login>
</user_info>
I want to use the acess_token authentication system to login the user in the website using PHP.
Here is the solution to my problem
$url = 'http://testapp.test/app.php/v1/verifyUser?username=admin&password=password&grant_type=password&client_id=6_20jeacfmdv0gwgg4kcgw44swwkoo8ogog8g4gwggwo0wko08ow&client_secret=38311bdlv884wc0ws4g048k0ieei8oc4oc0cmswok8w33o0s9e';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
$data = curl_exec($ch);
//echo $data;
if(curl_errno($ch))
{
print curl_error($ch);
}
else
{
curl_close($ch);
$xml = simplexml_load_string($data);
$json = json_encode($xml);
$var= json_decode($json,true);
}
if(isset($var["error"]))
{
echo "error";
}
else
{
$_SESSION['signed_in'] = true;
$_SESSION['sno'] = $var["user_id"];
$_SESSION['first_name'] = $var["firstname"];
}
Hopefully, this answer will help those who have same problem.
Google announced that all google plus api were deprecated on 7th march and I'm using google plus api with oauth2 so I'm worried about is my https://www.googleapis.com/plus/v1/people/me is also deprecated if yes than what is the alternative solution of that..
//index.php
<?php
include('constant.php');
include('google-login-api.php');
// include constant.php
// include google-api library
if(isset($_GET['code'])) {
$gapi = new GoogleLoginApi();
// Get the access token
$data = $gapi->GetAccessToken(GOOGLE_CLIENT_ID, GOOGLE_CLIENT_REDIRECT_URL, GOOGLE_CLIENT_SECRET, $_GET['code']);
if(is_array($data) && count($data)>0)
{
// Get user information
$user_info = $gapi->GetUserProfileInfo($data['access_token']);
echo "<pre>";
print_r($user_info);
}
}else{
//html code
?>
<a class="sign-in sign-google" href="<?php echo GOOGLE_API_URL; ?>"><i class="fa fa-google-plus"aria-hidden="true"></i>Sign In with Google</a>
<?php
}
?>
//google-login-api.php
<?php
class GoogleLoginApi
{
public function GetAccessToken($client_id, $redirect_uri, $client_secret, $code) {
$url = 'https://accounts.google.com/o/oauth2/token';
$curlPost = 'client_id=' . $client_id . '&redirect_uri=' . $redirect_uri . '&client_secret=' . $client_secret . '&code='. $code . '&grant_type=authorization_code';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$data = json_decode(curl_exec($ch), true);
$http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);
if($http_code != 200)
throw new Exception('Error : Failed to receieve access token');
return $data;
}
public function GetUserProfileInfo($access_token) {
$url = 'https://www.googleapis.com/plus/v1/people/me';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '. $access_token));
$data = json_decode(curl_exec($ch), true);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if($http_code != 200)
throw new Exception('Error : Failed to get user information');
return $data;
}
}
?>
here is my code which i have using and its run perfectly ...but the question is that Is still running after 7th march
A rough replacement for the Google Plus API (or at least the plus.people portion of the API) is the Google People API. It fundamentally does the same thing (returns profile information for users), although there are some changes in how the request is done and how the response is formatted. The biggest difference is that you need to request exactly what fields you want to receive.
In order to get the information for the user, you'd set $url with something more like
$fields = 'names,emailAddresses';
$url = 'https://people.googleapis.com//v1/people/me?personFields='+$fields;
You can see the reference for people.get for the possible values for the personFields parameter and the OAuth scopes that are valid for access.
Change url from,
$url = 'https://www.googleapis.com/plus/v1/people/me';
to,
$url = 'https://www.googleapis.com/oauth2/v3/userinfo';
I had same issue, solved here
I would like to make a small Fortnite API, but I always get an error in the JSON file.
{"message":"Invalid authentication credentials"}
My PHP Code:
$ch = curl_init();
//pc, xbl, psn
curl_setopt($ch, CURLOPT_URL, "https://api.fortnitetracker.com/v1/profile/pc/MyName");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'TRN-Api-Key: My-API-Code'
));
curl_setopt($ch, CURLOPT_VERBOSE, 1);
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
$fp = fopen("myStats.json", "w");
fwrite($fp, $response);
fclose($fp);
$data = json_decode(file_get_contents("myStats.json"));
$solo = $data->stats->p2;//solos data
$duos = $data->stats->p10;//duos data
$squads = $data->stats->p9;//squads data
$matches = $data->recentMatches;//match data
$sesh1 = $matches[0]->id->valueInt;
$solo_wins = $solo->top1->valueInt;
$duos_wins = $duos->top1->valueInt;
$squads_wins = $squads->top1->valueInt;
$solo_matches = $solo->matches->valueInt;
$duos_matches = $duos->matches->valueInt;
$squads_matches = $squads->matches->valueInt;
$solo_kd = $solo->kd->valueDec;
$duos_kd = $duos->kd->valueDec;
$squads_kd = $squads->kd->valueDec;
$solo_games = $solo->matches->valueInt;
$duos_games = $duos->matches->valueInt;
$squads_games = $squads->matches->valueInt;
$solo_kills = $solo->kills->valueInt;
$duos_kills = $duos->kills->valueInt;
$squads_kills = $squads->kills->valueInt;
$total_matches = ($solo_matches+$duos_matches+$squads_matches);
$total_wins = ($solo_wins+$duos_wins+$squads_wins);
$total_kills = ($solo_kills+$duos_kills+$squads_kills);
$total_kd = (round($total_kills/($total_matches-$total_wins),2));
echo 'Total Matches: '.$total_matches.'<br>';
echo 'Total Wins: '.$total_wins.'<br>';
echo 'Total Kills: '.$total_kills.'<br>';
echo 'Total KD: '.$total_kd.'<br>';
echo $sesh1;
?>
I entered the correct API code. Why is this message written in a JSON file and not my wins? It's so crazy because it should work.
What returns on line 11? You are using var_dump($response);
Could you write die(); under that line and provide us the return that it gives?
There might be something wrong on how you handle the headers. I can't check for sure right now as I am not home. But I might be able to test this out later.
I am trying to get 10 pages result listed using the following cod below. When i run the URL directly i get a json string but using this in code it does not returns anything. Please tell me where i am doing wrong.
$url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=CompTIA A+ Complete Study Guide Authorized Courseware site:.edu&start=20";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$body = curl_exec($ch);
curl_close($ch);
$json = json_decode($body,true);
print_r($json);
Now i am using the following code but it outputs only four entries of a page. Please tell me where i am doing wrong.
$term = "CompTIA A+ Training Kit Microsoft Press Training Kit";
for($i=0;$i<=90;$i+=10)
{
$term = $val.' site:.edu';
$query = urlencode($term);
$url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=' . $query . '&start='.$i;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$body = curl_exec($ch);
curl_close($ch);
$json = json_decode($body,true);
//print_r($json);
foreach($json['responseData']['results'] as $data)
{
echo '<tr><td>'.$i.'</td><td>'.$url.'</td><td>'.$k.'</td><td>'.$val.'</td><td>'.$data['visibleUrl'].'</td><td>'.$data['unescapedUrl'].'</td><td>'.$data['content'].'</td></tr>';
}
}
Just try with urlencode
$query = urlencode('CompTIA A+ Complete Study Guide Authorized Courseware site:.edu');
$url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=' . $query . '&start=20';
I managed to get this code so far:
<?php
//Gather data and prepare query
$thequery = urlencode($_GET['s']);
$yhost = 'http://boss.yahooapis.com';
$apikey = 'xxxxxxxxxxxxxxx';
$url = $yhost.'/ysearch/news/v1/'.$thequery.'?appid='.$apikey.'&format=xml';
//Get the results
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$results = new SimpleXmlElement($data, LIBXML_NOCDATA);
//echo the results
foreach ($results->resultset_news->result as $theresult) {
echo ''.$theresult->title.'<br/>';
echo $theresult->abstract.'<br/>';
echo '<small><i>'.$theresult->dispurl.'</i></small><br/>';
echo '<br/><br/>';
}
So how exactly do i output actual XML rather than HTML?
Tried this:
echo $results->asXML();
?