I found the Bitly API code below on this site. I'm having a hard time getting it to create and then echo a Bitly shortened URL for a variable called $fullurl. How would I do that?
EDIT: No error code appears, just no bitly shortened URL is shown.
EDIT 2: var_dump($response); returns NULL
EDIT 3: I did replace the API login and key with my mine.
EDIT 4: I found the answer in one of the comments on the original tutorial. My question was too basic for all you PHP pros: I simply needed to add echo bitly_shorten($fullurl); at the end.
Thanks in advance,
John
function bitly_shorten($url)
{
$query = array(
"version" => "2.0.1",
"longUrl" => $url,
"login" => API_LOGIN, // replace with your login
"apiKey" => API_KEY // replace with your api key
);
$query = http_build_query($query);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://api.bit.ly/shorten?".$query);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response);
if($response->errorCode == 0 && $response->statusCode == "OK") {
return $response->results->{$url}->shortUrl;
} else {
return null;
}
}
Change it to:
function bitly_shorten($url){
$query = array(
"version" => "2.0.1",
"longUrl" => $url,
"login" => API_LOGIN, // replace with your login
"apiKey" => API_KEY // replace with your api key
);
$query = http_build_query($query);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://api.bitly.com/v3/shorten?".$query);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response);
if( $response->status_txt == "OK") {
return $response->data->url;
} else {
return null;
}
}
It seems that bit.ly had updated their api , please visit
http://code.google.com/p/bitly-api/wiki/ApiDocumentation#Authentication_and_Shared_Parameters
for api..
The url seems to be something like this, http://api.bitly.com/v3/shorten?.....
The new version they stated is 3 and in your code its 2.0.1
Whenever you are using an api of an online service its better to get it from their site instead of getting that from any third party site or blog..
I found the answer in one of the comments on the original tutorial. My question was too basic for all you PHP pros: I simply needed to add echo bitly_shorten($fullurl); at the end.
Related
I am working with dr chrono API and am trying to initialized the First step which is authorisation
The API documentation is here.
Here is an Authorisation sample:
https://drchrono.com/o/authorize/?redirect_uri=REDIRECT_URI_ENCODED&response_type=code&client_id=CLIENT_ID_ENCODED&scope=SCOPES_ENCODED
Here is what I have tried:
1.) I have tried the first code below
<?php
echo "<a href='https://drchrono.com/o/authorize/?redirect_uri=https://example_site.com/return_page.php&response_type=code&client_id=myclient-id-goeshere&scope=BASE_SCOPE:[read|write]'>Authorize</a>";
?>
but when the page redirects it displays error of Invalid_scope. Below is the error link returned.
https://example_site.com/return_page.php?error=invalid_scope
2.) Using Curl
$ci = 'my-client-id-goes-here';
$ci_encode = urlencode($ci);
$uri = 'https://example_site.com/return_page.php';
$uri_encode = $uri;
$url = "https://drchrono.com/o/authorize/";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
'response_type'=>'code',
'client_id'=>$ci_encode,
'redirect_uri'=>$uri_encode,
'scope'=>'BASE_SCOPE:[read|write]',
]));
$response = curl_exec($ch);
curl_close($ch);
print_r($response);
Using curl code above does even redirect me at all.
I think the major problem is the scope not properly set. How can I solve this issue?
Update
Code section for curl:
$ci = 'my-client-id';
$ci_encode = urlencode($ci);
$uri = 'https://example_site.com/return_page.php';
$uri_encode = $uri;
$url = "https://drchrono.com/o/authorize/?";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
'response_type'=>'code',
'client_id'=>$ci,
'redirect_uri'=>$uri,
'scope'=>'patients:summary:read patients:summary:write calendar:read calendar:write clinical:read clinical:write'
]));
$response = curl_exec($ch);
curl_close($ch);
print_r($response);
Instead of BASE_SCOPE:[read|write] I would use patients:summary:read patients:summary:write calendar:read calendar:write clinical:read clinical:write
The docs say that you can choose among user, calendar, patients, patients:summary, billing, clinical and labs and compose a scope out of these values.
I need get photos and descriptions from registered instagram.
As I understand from docs on github https://github.com/cosenary/Instagram-PHP-API
You need put auth link first ant then using auth token after click on it you are redirected to some callback url, where you can get media then. But can I somehow login automatically without clicking link??
I have some
$instagram = new Instagram(array(
'apiKey' => 'apiKey',
'apiSecret' => 'apiSecret',
'apiCallback' => 'apiCallback there'
));
with my data get after registration in https://www.instagram.com/developer/. How can I get what I want?
I guess try something like this:
$userid = "User-ID-Goes-Here";
$accessToken = "Token-Goes-Here";
// Gets our data
function fetchData($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
// Pulls and parses data.
$result = fetchData("https://api.instagram.com/v1/users/{$userid}/media/recent/?access_token={$accessToken}");
$result = json_decode($result);
//use data here
Source: http://blueprintinteractive.com/blog/how-instagram-api-fancybox-simplified
I have a web application which needs to fetch the real time updates to a particular page. I have gone through a lot of questions on this forums and yet not found anything that works for me. When I subscribe to the page updates
I am supplying valid app_id, app_secret and app_url.
<?php
$app_id = '';
$app_secret = '';
$app_url = '';
$fields = 'feed';
$verify_token = 'abcd#123';
// Fetching an App Token
$app_token_url = 'https://graph.facebook.com/oauth/access_token?client_id='
.$app_id.'&client_secret='.$app_secret
.'&grant_type=client_credentials';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $app_token_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($ch);
parse_str($res, $token);
if (isset($token['access_token'])) {
// Let's register a callback
$params = array(
'object'
=> 'page',
'fields'
=> $fields,
'callback_url'
// This is the endpoint that will be called when
// a User updates the location field
=> $app_url . '/index.php?action=callback',
'verify_token'
=> $verify_token,
);
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/'
.$app_id.'/subscriptions?access_token='
.$token['access_token']);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$res = curl_exec($ch);
if ($res && $res != 'null') {
print_r($res);
}
// Fetch list of all callbacks
curl_setopt($ch, CURLOPT_POST, 0);
$res = curl_exec($ch);
}
if ($res && $res != 'null') {
print_r($res);
error_log('updates = ' . print_r($res, true));
}
curl_close($ch);
?>
the FB posts to my callback URL but doesn't send the id of the post which was updated, instead it sends the user id in both the id as well as the uid. Even the updates are irregular, sometimes there is a notification, at other times no notification.
What do I need to do make this work?
There is something about whitelisting apps -- does that need to be done.
Do I need to make this app as a facebook app and have the page install this app on a tab?
Do I need special permission to be granted by the page admin.
Can this be done at all?
Any help would be very welcome. Thanks!!
A similar question on this forum :Facebook Real Time Update return only "changed_fields":["feed"] and not the actual comment
Using Facebook Graph API and my app credentials I am able to retrieve a valid token:
171372936213670|xxxxxxxxxxxxxxxxxx-650901429|x_xxxxxxxxxxxxxxx-xxxxxxxx
The code I use to retrieve Wall Posts:
function getFbWallPosts($user, $limit=5) {
$ci =& get_instance();
$token = $ci->facebook->getAccessToken();
$param = array(
'access_token' => $token,
'limit' => $limit,
);
$posts = $ci->facebook->api("$user/feed", 'GET', $param);
return $posts;
}
The function always returns an empty jSON result:
{"data":[]}
Also tested directly accessing Wall Posts using cURL:
function fbrequest($url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
...to no avail.
EDIT: Also tried using file_get_contents but it also returns empty jSON:
function fbrequest($url) {
$output = file_get_contents($url);
return $output;
}
Tried pasting the request string directly on my browser (without a user logged in to Facebook):
https://graph.facebook.com/AValidFacebookUsername/feed?access_token=171372936213670|xxxxxxxxxxxxxxxxxx-650901429|x_xxxxxxxxxxxxxxx-xxxxxxxx
...successfully returns a jSON string with all the posts
Any ideas guys?
Try...
function curl($url){
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
$content = curl_exec($curl);
curl_close($curl);
return $content;
}
If no luck, maybe Apache is not compiled with SSL support.
I just found out that there is nothing wrong with the codes I'm writing. The problem is with how I interpreted the process and, eventually, the result. There are pre-requisites on accessing other user's data on FB and in case of group pages, one of them is that the user requesting the data MUST BE A FAN OF THAT GROUP.
Silly me...
always: https://www.google.com/accounts/o8/ud
i got wordpress openid ok. so i think is is just discovery phase got some probelms..
<?php $ch = curl_init();
$url = 'https://www.google.com/accounts/o8/id';
$url = $url.'?';
$url = $url.'openid.mode=checkid_setup';
$url = $url.'&openid.ns=http://specs.openid.net/auth/2.0';
$url = $url.'&openid.claimed_id=http://specs.openid.net/auth/2.0/identifier_select';
$url = $url.'&openid.identity=http://specs.openid.net/auth/2.0/identifier_select';
$url = $url.'&openid.return_to='.site_url().'/user/openid/login_callback';
$url = $url.'&openid.realm=http://www.example.com/';
// set url
curl_setopt($ch, CURLOPT_URL, $url);
//return the transfer as a string
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER,array("Accept: */*"));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
// $output contains the output string
$xdr = curl_exec($ch);
if (!$xdr) {
die(curl_error($ch));
}
// close curl resource to free up system resources
curl_close($ch);
$xml = new SimpleXMLElement($xdr);
$url = $xml->XRD->Service->URI;
$request = $connection->begin($url);
$request always null...
Take a look at https://blog.stackoverflow.com/2009/11/google-offers-named-openids/ where Jeff explains this behavior and what the user can do about it:
Well, the good news is, now you can! Google just gave us a fantastic Thanksgiving Day present in the form of Google Profiles supporting OpenID. And with a Google Profile, you get to pick a named URL of your choice!
Your question has the right endpoint URL (the one ending in /ud), but your example code is sending the request to the identifier URL (/id), not the endpoint URL.
My above code do return https://www.google.com/accounts/o8/ud in $url, which is correct actually
the problem is, you do not need to use openid php lib, just redirect the user to https://www.google.com/accounts/o8/ud with query string like:
https://www.google.com/accounts/o8/ud?openid.mode=checkid_setup&......