Having trouble with methods for Vimeo's new API - php

I'd like to use the method vimeo.videos.getInfo to get the info on a private video uploaded to my account. I'll only ever use this application on one website, so I'm hard coding the access token into the code.
I'm using the official PHP library for the Vimeo API (https://github.com/vimeo/vimeo.php).
So here's what I have so far...
$vimeo = new Vimeo($apiKey, $apiSecret, $accessToken);
All good. At first, when I tried the sample code from the example:
$user_data = $vimeo->request('/me');
print_r($user_data);
That returned an empty array:
Array (
[body] =>
[status] => 0
[headers] => Array
(
)
)
I noticed they mentioned if the array is returning empty, it probably had something to do with an invalid SSL certificate. Right now, I'm just developing on localhost, so I set CURLOPT_SSL_VERIFYPEER to false (thanks to these instructions: http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/). I added it to the array on line 112 in vimeo.php:
$curl_opt_defaults = array(
CURLOPT_HEADER => 1,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 30,
CURLOPT_SSL_VERIFYPEER => false);
As soon as I did that, I was able to return the information about the authenticated user.
But this still is returning an empty array:
$params = array(
'video_id' => 95307197
);
$videos = $vimeo->request('vimeo.videos.getInfo', $params);
print_r($videos);
Same with any methods I try to put in there. Did I do the CURLOPT_SSL_VERIFYPEER thing wrong or is something else wrong with my syntax?

I thought that I might share my solution which took me some time to figure out. I also wanted to access private video data, namely the number pf time the video played. Here are my steps:
Create an app on Vimeo and get the Client Identifier, Client Secret, and generate an Access Token with the properties Public, Private, and Interact. You may need to add or remove properties based on your access need.
Download from Vimeo the PHP source code.
Strangely, at least in my case, the code had a wrong syntax on line 473 in the statement $name = array_slice(explode("/", $file_path), -1)[0]; to solve it remove the [0] at the end! In fact I didn't need to call this function to know if it did any harm, but this solved my problem. Am on Dreamweaver by the way.
More strangely, the PHP code provided my Vimeo can not do authentication with their new system so you need to add this code curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); before every $response = curl_exec($curl); statement. This should be 3 additions.
Finally, create your own PHP script to access the private video.
Here is my Code:
require("../Vimeo PHP path/autoload.php");
use Vimeo\Vimeo;
$client_id = "*****"; //your Vimeo number here
$client_secret = "*****"; //your Vimeo number here
$access_token = "*****"; //your Vimeo number here
$vim = new Vimeo($client_id, $client_secret, $access_token);
$response = $vim->request("/videos/****"); //your Vimeo PRIVATE video ID here
echo $response["body"]["stats"]["plays"];
In my case I did echo for the number of plays only, but you can print the whole body. Hope this helps someone too.

Thanks to this example I solved the same issue, in the current api at _request method(line 125) add the curl option CURLOPT_SSL_VERIFYPEER and set it to false so you will have an array like this:
$curl_opt_defaults=array(CURLOPT_HEADER => 1,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 30,
CURLOPT_SSL_VERIFYPEER=>false);
and that's all :) hope this helps someone else.

Hopefully this helps someone else. The Vimeo API documentation is all out of whack and the new API docs link back to the older API docs, which only adds to the confusion.
The new API doesn't use the methods from the Advanced API, it uses the endpoints here https://developer.vimeo.com/api/endpoints
Here is the code that eventually worked for me:
$videos = $vimeo->request("/videos/$video_id");
print_r($videos);

Related

Pulling content from Instagram using just cUrl

I am trying to show Instagram content from a user (mines) on a website. My client-side page would make an AJAX request to my server-side script, which in turn, would do the necessary stuff to pull the content from Instagram API (using cUrl) and send back a JSON response.
Sadly, I am little bit confused with the Authentication instructions, particularly Step One: Direct your user to our authorization URL.
In order to get an access_token to work with the API, I need to pass client_id, redirect_uri, and code. I have the client_id and redirect_uri. The problem is the last item, code, which gets found after redirecting from https://api.instagram.com/oauth/authorize/?client_id=CLIENT-ID&redirect_uri=REDIRECT-URI&response_type=code. The redirected page will be REDIRECT-URI?code=CODE which has the CODE to use to make the request to get the access_token.
Is there a way to "automate" this last part ? Or more precisely, is it possible to make a request (using cUrl) and then grabbing this CODE
Hopefully, this is clear. Unlike other APIs, Instagram seems to make things difficult. My goal is so that when users come on the page, they will see a gallery of images from my Instagram account. They shouldn't need to log into Instagram or do anything to see these images.
Edit
$clientId = 'XXX';
$clientSecret = 'YYY';
$redirectUri = 'ZZZ';
// step 1: authorize
$url = "https://instagram.com/oauth/authorize/?client_id={$clientId}&redirect_uri={$redirectUri}&response_type=code";
try {
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_CONNECTTIMEOUT => 30,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_URL => $url
));
$data = json_decode(curl_exec($ch), true);
// how to get CODE from response?
curl_close($ch);
} catch(Exception $e) {
echo $e->getMessage();
}

Soundcloud resolve on PHP

I'm trying to setup a simple app that should:
get the client ID of an artist from its URL
display the two latest tracks
However I'm not that practical with soundcloud and i just know basic php. I started to play with soundcloud but i wasn't able to handle it. A problem i have is that any code i write, it gets
Fatal error: Uncaught exception 'Services_Soundcloud_Invalid_Http_Response_Code_Exception' with message 'The requested URL responded with HTTP code 302.'
The easiest setup come straight from the documentation and is an example to retrieve the comments from the track id, starting from a give URL.
<?php
require_once 'Services/Soundcloud.php';
// create a client object with your app credentials
$client = new Services_Soundcloud('my_client','my_secret');
// a permalink to a track
$track_url = 'https://url_to_a_track';
// resolve track URL into track resource
$track = json_decode($client->get('resolve', array('url' => $track_url), array('CURLOPT_FOLLOWLOCATION', TRUE )));
// now that we have the track id, we can get a list of comments, for example
foreach (json_decode($client->get('tracks/' . $track->id . 'comments')) as $c)
print 'Someone said: ' . $c->body . ' at ' . $c->timestamp . "\n"; ?>
Just added ('CURLOPT_FOLLOWLOCATION', TRUE) because I've read about it around the web... And I always get the fatal error... why?
'How can I use the soundcloud API resolve resource with PHP?'
to use the resolve resource of the soundcloud API with php, following frafor's code, do:
$client->setCurlOptions(array(CURLOPT_FOLLOWLOCATION => 1));
$response = json_decode($client->get('resolve',array('url' => $track_url)));
?>
So first CURLOPT_FOLLOWLOCATION and then the API call, your where almost there! :)
Hope it helps someone!
Cheers,
T
the soundcloud servers have switched to secure and they now use https protocol for API / JSON as well.
My apps were not working any more so these are the other cURL options to get the JSON.
SSL verification must both be set to disable. I got it working only with this.
// Configuring curl options
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('Content-type: application/json'),
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
);
Hope it helps.
I've been able to solve it. If the resources are public, then you don't need to authenticate. Here's the code that:
Get user URL from a wordpress custom-field
Retrieve ID from json object
Retrieve the latest 2 tracks and display 2 embedded players
It just need your own Client ID you can get easily by registering on soundcloud developers section, and then substitute it to {Your_ID}
// get user URL from Wordpress custom field
$sc_url = get_post_meta(get_the_id(), 'sc_url', true);
// if $sc_url is not empty, do
if (!empty($sc_url)) {
$unparsed_json = file_get_contents('https://api.soundcloud.com/resolve.json?url='.$sc_url.'&client_id={Your_ID}');
$json_object = json_decode($unparsed_json);
// retrieve the user ID from json_object
$roster_id = $json_object->{'id'};
// get last two tracks from the user and generate embed code for each tracks
$tracks_json = file_get_contents('http://api.soundcloud.com/users/'.$roster_id.'/tracks?client_id={Your_ID}&order=latest&limit=2&format=json');
$tracks = json_decode($tracks_json);
foreach ($tracks as $track){
$trackID = $track->id;
echo '<iframe width="100%" height="166" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F'.$trackID.'"></iframe>';
}}
Hope it can help others :)

login with facebook using oauth

<?php
require "facebook.php";
$facebook = new Facebook(array(
'appId' => '3288##########',
'secret' => 'ca2##################',
));
$user = $facebook->getUser();
I have made the app in the graph api and I have very little knowledge of graph api . I want to make a facebook login page in which user clicked on it and my app will generate the oauth for the user . after that i need the USERNAME, EMAIL,BIRTHDAY,NAME in the fre filled forms
I'm searching for this code from 3 night, but i didn't find the solution! If you have a suggestion, please write to me it! Please, anyway thanks and good day :)
You could use POST request to simulate login. I don't know Facebook's inputs, so you need to look closely into it's HTML, but here is a code for a simple PHP cURL request.
// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://www.facebook.com/login.php',
CURLOPT_USERAGENT => 'Facebook Login',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
'username' => 'username',
'password' => 'myfbpass'
)
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
Of course, Facebook has automatic login detection by using those hidden fields for getting the sessions, but using DOM Document, you can easily get those values and emulate it, at least thats how I was able to simulate login for http://z8games.com/
Good luck
Code Credit:
http://codular.com/curl-with-php
Edit:
If you are lazy, you can go ahead and try this page - http://www.daniweb.com/web-development/php/code/290893/facebook-login-with-curl
Temboo makes it simple to implement Facebook OAuth by breaking the process down into two steps:
ÌnitializeOAuth returns the Facebook authorization URL that you need to show your users so that they can grant your app access to their Facebook accounts.
FinalizeOAuth returns the access token you need to programmatically read/write a user's Facebook data.
What's more, you can test these calls from your browser and generate the source code you need in the language of your choice.
Here's a short video that shows you how to do this with Temboo, and you can check out an example and source code here.
(Full disclosure: I work at Temboo, so let me know if you have any questions!)

Call Instagram API from localhost

Is it possible to get INSTAGRAM user details through API from localhost ?
Because I am trying to doing this from localhost but unable to fetch the information.
After click the login url it redirect me to the INSTAGRAM and after successfully login and authorize the apps it redirect me properly in my callback URL with code. But when I am trying to fetch the info (cURL) using access token then it return null.
But same code run perfectly in live.
Make sure the Curl option CURLOPT_SSL_VERIFYPEER is set to false. Find the Curl request in your code and add or edit an option that will look something like this:
$ch = curl_init();
...
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); <<-- Add this line
Ultimately I resolved the problem. now I am able to connect to Instagram.
I use the CodeIgniter Instagram Library by Ian Luckraft.
In config file I just change the
$config['instagram_ssl_verify'] = FALSE;
By default it was TRUE.
Yes it is possible.
You can try using Instagram api wrapper which is made by Galen Grover
https://github.com/galen/PHP-Instagram-API
Mine's works just fine
in instagram .config.php in your localhost
you must set 'apiCallback' =>
to 'http://localhost/instagram/instagram/'
folder of your success.php
<?php
// Setup class
$instagram = new Instagram(array(
'apiKey' => '',
'apiSecret' => '',
'a
?>piCallback' => 'http://localhost/instagram/instagram/' // must point to success.php
));

Facebook PHP SDK - getUser() suddenly returns 0

First of all, to get this out of the way, I've spent hours looking through answers to this question here on StackOverflow, and tried to solve it on my own, but it just isn't working (anymore). So anyway...
Yesterday, everything worked fine. Went to bed, woke up, login with facebook didn't work anymore on 2 of my websites. The login link sends me to the Facebook page which requires me to approve the App, but after it redirects me back to the site, it doesn't log me in. I quickly tracked the problem to getUser() always returning 0. So I set up a new (test) app with a very very basic code, just to see if that works. It doesn't. Here's the code:
<?php
session_start();
require_once("facebook.php");
$config = array();
$config['appId'] = '199971880127910'; //test app so I don't mind showing these
$config['secret'] = 'e065588cb1edd96f821de8b3d362c488';
$config['trustForwarded'] = true;
$facebook = new Facebook($config);
try {
$user = $facebook->getUser();
var_dump($user);
} catch (Exception $e) {
$e->getMessage();
}
$loginUrl = $facebook->getLoginUrl(array('redirect_uri' => 'http://gtcrais.redirectme.net/test/'));
echo "<a href='".$loginUrl."'>Login</a>";
?>
The second answer in here caught my eye. I tried that solution and it didn't work, so I opened up base_facebook.php and scrolled to the line where that 'code' is passed to some variable - line 458:
$code = $this->getCode();
I echo'd this, and it displayed correctly the code parameter that was passed from the URL. The problematic line turns out to be the one couple rows below that one:
$access_token = $this->getAccessTokenFromCode($code);
This function returns a value of false to $access_token when passed the $code variable. In that function, this part of the code throws an exception:
try {
// need to circumvent json_decode by calling _oauthRequest
// directly, since response isn't JSON format.
$access_token_response =
$this->_oauthRequest(
$this->getUrl('graph', '/oauth/access_token'),
$params = array('client_id' => $this->getAppId(),
'client_secret' => $this->getAppSecret(),
'redirect_uri' => $redirect_uri,
'code' => $code));
} catch (FacebookApiException $e) {
// most likely that user very recently revoked authorization.
// In any event, we don't have an access token, so say so.
print_r($e);
return false;
}
print_r($e) displays a huge blob of gibberish (I'm somewhat of a noob so I can't read almost anything from it that makes sense). The only thing that I see repeating in there is
SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL
And this is how far I've come with debugging this. Any help is appreciated. Once again, yesterday, everything was working fine. Today, without a single line of code changed, it doesn't work anymore. Mind boggling =( Oh yeah, I'm running WAMP with PHP 5.4.3
EDIT: Here's the error message: http://i.imgur.com/TeY8h.png
EDIT2: Solved:
In base_facebook.php add the following 2 lines under $CURL_OPTS:
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => 2
Which makes it...
public static $CURL_OPTS = array(
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 60,
CURLOPT_USERAGENT => 'facebook-php-3.2',
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => 2
);
I'm not sure how much of a security issue this is, but it makes the Login with Facebook work, so there you have it (and there I have it. Entire day lost on this, fml).
EDIT3: New SDK released which fixes this problem: https://github.com/facebook/facebook-php-sdk
In base_facebook.php add the following 2 lines under $CURL_OPTS:
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => 2
Which makes it...
public static $CURL_OPTS = array(
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 60,
CURLOPT_USERAGENT => 'facebook-php-3.2',
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => 2
);
I'm not sure how much of a security issue this is, but it makes the Login with Facebook work, so there you have it.
EDIT: New SDK released which fixes this problem: https://github.com/facebook/facebook-php-sdk
I think you need to setAccessToken() before calling any facebook methods, even when the use has authorized your app and is logged in.
I had the same problem. It turned out i was using localhost. Though the allow access dialog appeared on facebook and i was redirected back to my web page, getUser still returned zero.
My Solution
Once i uploaded my code and tested it online, reasonable facebook errors appeared then had to change my "App Domains" and "Site Url" to relevant values for my online test website NOT localhost. And then it worked!
I had this issue, which turned out to be a corrupt certificate. I think I had autocrlf turned on in Git. I don't know if that fixed it or not, but blowing away my whole repo and re-cloning from source caused this problem to clear right up!

Categories