Why doesn't anything appear on my web page when I use Instagram API? - api

I'm trying to use Instagram API on my web page to get user information, but when I use the API, I see only a blank page.
My code was working a few days ago, what might the problem be?
require_once 'instagram.class.php';
$instagram = new Instagram('xxx');
$tag = 'tag';
$media = $instagram->getTagMedia($tag);
$limit = 50;
foreach(array_slice($media->data, 0, $limit) as $data)
{
$image = $data->images->standard_resolution->url;
$avatar = $data->user->profile_picture;
$username = $data->user->username;
if ($image <> '') {
$sqlMy = mysql_query("SELECT * FROM table WHERE image='$image'");
$sorgula = mysql_num_rows($sqlMy);
if($sorgula=="0")
{
$yaz2=mysql_query("INSERT INTO table (id,image,avatar,username) VALUES ('','$image','$avatar','$username')");
}
}
}

Related

modify relationship in instagram by using oriceon laravel 5 oauth wrapper

Iam using oriceon/oauth-5-laravel for connecting with instagram.I authenticated and i got my name and id by through this request
$result = json_decode($instagramService->request('users/self'), true);
Now i want to follow a person by using this oriceon/oauth-5-laravel.
How i could post the request to follow a person by using this .
**NOTE:**dont refer the instagram developer page.I reffred it a lot.But i couldnt put a post request for ORICEON/laravel oauth wrapper for instagram.
HELP ME.TYSM in advance
try this .Copy this method to your controller and do proper routing.
public function followWithInstagram(Request $request)
{
$instagram_id='id of the user that you want to follow';
$code = $request->get('code');
$instagramService = \OAuth::consumer('Instagram');
if ( ! is_null($code))
{
$state = isset($_GET['state']) ? $_GET['state'] : null;
$instagramService->requestAccessToken($code, $state);
$linkData = [
'action' =>'follow',
];
$result =json_decode($instagramService->request('https://api.instagram.com/v1/users/'.$instagram_id.'/relationship','POST',$linkData),true);
if(!$result){
return ("Failed");
}
else {
return ("SUCCESS");
}
} else {
$url = $instagramService->getAuthorizationUri();
return redirect((string)$url);
}
}

Magento: listing products from API

This is my script I have used from Magento Reference (see: RequestToken Retrieve the list of products for Admin user with OAuth authentication)
I am submiting this script from my localhost.
/**
* Example of retrieving the products list using Admin account via Magento REST API. OAuth authorization is used
* Preconditions:
* 1. Install php oauth extension
* 2. If you were authorized as a Customer before this step, clear browser cookies for 'yourhost'
* 3. Create at least one product in Magento
* 4. Configure resource permissions for Admin REST user for retrieving all product data for Admin
* 5. Create a Consumer
*/
// $callbackUrl is a path to your file with OAuth authentication example for the Admin user
$callbackUrl = "http://yourhost/oauth_admin.php";
$temporaryCredentialsRequestUrl = "http://yourhost/oauth/initiate?oauth_callback=" . urlencode($callbackUrl);
$adminAuthorizationUrl = 'http://yourhost/admin/oAuth_authorize';
$accessTokenRequestUrl = 'http://yourhost/oauth/token';
$apiUrl = 'http://yourhost/api/rest';
$consumerKey = 'yourconsumerkey';
$consumerSecret = 'yourconsumersecret';
session_start();
if (!isset($_GET['oauth_token']) && isset($_SESSION['state']) && $_SESSION['state'] == 1) {
$_SESSION['state'] = 0;
}
try {
$authType = ($_SESSION['state'] == 2) ? OAUTH_AUTH_TYPE_AUTHORIZATION : OAUTH_AUTH_TYPE_URI;
$oauthClient = new OAuth($consumerKey, $consumerSecret, OAUTH_SIG_METHOD_HMACSHA1, $authType);
$oauthClient->enableDebug();
if (!isset($_GET['oauth_token']) && !$_SESSION['state']) {
$requestToken = $oauthClient->getRequestToken($temporaryCredentialsRequestUrl);
$_SESSION['secret'] = $requestToken['oauth_token_secret'];
$_SESSION['state'] = 1;
header('Location: ' . $adminAuthorizationUrl . '?oauth_token=' . $requestToken['oauth_token']);
exit;
} else if ($_SESSION['state'] == 1) {
$oauthClient->setToken($_GET['oauth_token'], $_SESSION['secret']);
$accessToken = $oauthClient->getAccessToken($accessTokenRequestUrl);
$_SESSION['state'] = 2;
$_SESSION['token'] = $accessToken['oauth_token'];
$_SESSION['secret'] = $accessToken['oauth_token_secret'];
header('Location: ' . $callbackUrl);
exit;
} else {
$oauthClient->setToken($_SESSION['token'], $_SESSION['secret']);
$resourceUrl = "$apiUrl/products";
$oauthClient->fetch($resourceUrl, array(), 'GET', array('Content-Type' => 'application/json'));
$productsList = json_decode($oauthClient->getLastResponse());
print_r($productsList);
}
} catch (OAuthException $e) {
print_r($e->getMessage());
echo "<br/>";
print_r($e->lastResponse);
}
As a result I am getting "Whoops, our bad..." page, I was expecting to see list of products.
In the URL I am seeing oauth_token.
http://myUrl/oAuth_authorize?oauth_token=a4315776966dca4fa5d3786f4fghjkab157cf
Can somebody point me out what to do next? If you need any additional data, please ask.
There is a typo in url
$adminAuthorizationUrl = 'http://yourhost/admin/oAuth_authorize';
should be
$adminAuthorizationUrl = 'http://yourhost/admin/oauth_authorize';

Google Plus Login API not working on production server

I have implemented the google plus api on development server and it works fine. I used the same code on production server. But after requesting the permission it takes a long time to return to my site and login.
Can anyone please let me know what might be the cause. I have used oauth2.
Below is the code I am using
<?php
session_start();
require_once 'googleplus/src/Google_Client.php';
require_once 'googleplus/src/contrib/Google_Oauth2Service.php';
class Webpage_UserGPlusLogin extends Webpage
{
public function __construct()
{
$temp_redirect = $_SESSION['RETURN_URL_AFTERLOGIN'];
$this->title = 'User Account';
$client = new Google_Client();
$client->setApplicationName(WEBSITE_NAME);
$client->setClientId(GOOGLE_PLUS_CLIENT_ID); // Client Id
$client->setClientSecret(GOOGLE_PLUS_CLIENT_SECRET); // Client Secret
$client->setRedirectUri(GOOGLE_PLUS_REDIRECT_URI); // Redirect Uri set while creating API account
$client->setDeveloperKey(GOOGLE_PLUS_DEVELOPER_KEY); // Developer Key
$oauth2 = new Google_Oauth2Service($client);
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
$redirect = GOOGLE_PLUS_REDIRECT_URI;
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL)); // Redirects to same page
return;
}
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
if (isset($_REQUEST['logout'])) {
unset($_SESSION['token']);
$client->revokeToken();
}
if(!isset($_SESSION['email_address_user_account'])) // Check if user is already logged in or not
{
if ($client->getAccessToken()) {
$user = $oauth2->userinfo->get(); // Google API call to get current user information
$email = filter_var($user['email'], FILTER_SANITIZE_EMAIL);
$img = filter_var($user['picture'], FILTER_VALIDATE_URL);
$googleuserid = $user['id'];
$given_name = $user['given_name'];
$family_name = $user['family_name'];
// The access token may have been updated lazily.
$_SESSION['token'] = $client->getAccessToken();
// If email address is present in DB return user data else insert user info in DB
$this->result = UserAccount::gplus_sign_up($email, $googleuserid, $given_name, $family_name);
// Create new user object.
$this->user_account = new UserAccount($this->result['id'],$this->result['email_address'],$this->result['password'],$this->result['confirmation_code'],$this->result['is_confirmed'], $this->result['first_name'], $this->result['last_name']);
$_SESSION['gplus_email_address'] = $email;
$_SESSION['gplus_first_name'] = $given_name;
$_SESSION['gplus_last_name'] = $family_name;
$_SESSION['gplus_id'] = $googleuserid;
$_SESSION['gplus_profile_pic'] = $img;
$_SESSION['email_address_user_account'] = $email;
} else {
$authUrl = $client->createAuthUrl();
}
}
if(isset($temp_redirect))
header("Location:".$temp_redirect);
else
header("Location:/");
}
}
Thanks in advance
Try this
use following code
$temp = json_decode($_SESSION['token']);
$request = "https://www.googleapis.com/oauth2/v1/userinfo?alt=json";
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,$request);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl,CURLOPT_TIMEOUT,30);
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
curl_setopt($curl,CURLOPT_HTTPHEADER,array('Authorization: OAuth '.$temp->access_token));
$response = trim(curl_exec($curl));
$info = curl_getinfo($curl);
$request_header_info = curl_getinfo($curl, CURLINFO_HEADER_OUT);
//var_dump($info);
//var_dump($request_header_info);
curl_close($curl);
echo "<pre>";
print_r(json_decode($response));
instade of
$user = $oauth2->userinfo->get(); // Google API call to get current user information`enter code here`
Hope this will help you .. :)

Vimeo API to fetch video with vanity url?

Does anybody know how to get a response from Vimeo's api for vanity / custom urls like the following:
http://vimeo.com/jarominne/regarde-moi or http://vimeo.com/davidfield/caterpillar
The standard Api request for a video id doesn't work.
Does not work: https://vimeo.com/api/v2/video/regarde-moi.json
Works: https://vimeo.com/api/v2/video/58086788.json
I don't see anything that makes sense or is a clear cut solution in their docs. I don't care about language.
FWIW, this is a crappy php solution I just quickly thought of shortly after asking this question.
Improvements very welcome!
Essentially, if we do not have a video id (a numeric value), assume our id is a video title. Fetch our user data with the username from the url and grab all their videos. Loop through those videos and try and match the title with our "video id" (which in the case above is really the video title).
** assumes $this->id = your video id or video username
** assumes $this->user = your vimeo username
public $apis = array(
'vimeo_image' => 'http://vimeo.com/api/v2/video/%s.json',
'vimeo_user' => 'http://vimeo.com/api/v2/user/%s/videos.json'
);
public function vimeoThumbnail($size = 'small')
{
if(preg_match('/[0-9]+/', $this->id)) {
$video_id = $this->id;
} else {
$videos = file_get_contents(sprintf($this->apis['vimeo_user'], $this->video->user));
if($videos !== false) {
$videos = json_decode($videos);
foreach($videos as $video) {
if(stristr($video->title, $this->id)) {
$video_id = $video->id;
break;
}
}
}
}
$contents = file_get_contents(sprintf($this->apis['vimeo_image'], $video_id));
if($contents !== false) {
$contents = json_decode($contents);
if(is_array($contents)) $contents = array_shift($contents);
if(!is_object($contents)) return false;
$image_url = $contents->{'thumbnail_' . $size};
return $image_url;
}
return false;
}

Joomla onUserAuthenticate

In the Joomla source, I found a method caled onUserAuthenticate, which could not be found in the API (through google), but its functionality is the similar to onLoginUser... So, after login/password check I need to run some more code via this function. As a result, I have true/false - depending on it I need to set users' authorization completely. Even if the user's login/password is correct, but my code returns false -> authorization fail...
I am trying something like:
functionon UserAuthenticate($credentials,$options,&$response){
jimport('joomla.user.helper');
$username=mysql_real_escape_string($credentials['username']);
$password=mysql_real_escape_string(md5($credentials['password']));
//my code returns $result
if($result!=NULL){
$response->status=JAUTHENTICATE_STATUS_SUCCESS;
$response->error_message='';
}
else{
$response->status=JAUTHENTICATE_STATUS_FAILURE;
$response->error_message=JText::_('JGLOBAL_AUTH_INVALID_PASS');
}
}
onUserAuthenticate is an event not a method. You use plugins to listen for Joomla events, in this case usually a user plugin would listen for this. When the event happens your code will run.
http://docs.joomla.org/Plugin
You can try this for custom login form-
$app = JFactory::getApplication();
$data = array();
$data['return'] = '';
$data['username'] = JRequest::getVar('username', '', 'method', 'username');
$data['password'] = JRequest::getString('password', '', 'post', JREQUEST_ALLOWRAW);
// Get the log in options.
$options = array();
// Get the log in credentials.
$credentials = array();
$credentials['username'] = $data['username'];
$credentials['password'] = $data['password'];
// Perform the log in.
$error = $app->login($credentials, $options);
if (!JError::isError($error)) {
$response->status=JAUTHENTICATE_STATUS_SUCCESS;
$response->error_message='';
}else{
$response->status=JAUTHENTICATE_STATUS_FAILURE;
$response->error_message=JText::_('JGLOBAL_AUTH_INVALID_PASS');
}
If you want authenticate solution on function "onUserAuthenticate" you should check it yourself if user credential is valid or not And you do it with this code :
function onUserAuthenticate($credentials, $options, &$response)
{
$response->type = 'Joomla';
// Joomla does not like blank passwords
if (empty($credentials['password'])) {
$response->status = JAuthentication::STATUS_FAILURE;
$response->error_message = JText::_('JGLOBAL_AUTH_EMPTY_PASS_NOT_ALLOWED');
return false;
}
// Initialise variables.
$conditions = '';
// Get a database object
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('id, password');
$query->from('#__users');
$query->where('username=' . $db->Quote($credentials['username']));
$db->setQuery($query);
$result = $db->loadObject();
if ($result) {
$parts = explode(':', $result->password);
$crypt = $parts[0];
$salt = #$parts[1];
$testcrypt = JUserHelper::getCryptedPassword($credentials['password'], $salt);
if ($crypt == $testcrypt) {
$user = JUser::getInstance($result->id); // Bring this in line with the rest of the system
$response->email = $user->email;
$response->fullname = $user->name;
$response->status = JAuthentication::STATUS_SUCCESS;
$response->error_message = '';
print_r("You login correct Sir");
die();
} else {
print_r("you enter wrong credential");
die();
$response->status = JAuthentication::STATUS_FAILURE;
$response->error_message = JText::_('JGLOBAL_AUTH_INVALID_PASS');
}
} else {
print_r("you enter blank credential");
die();
$response->status = JAuthentication::STATUS_FAILURE;
$response->error_message = JText::_('JGLOBAL_AUTH_NO_USER');
}
return true;
}