Bitly API causing unexpected illegal token - php

I'm using the Bit.ly API to generate a shorturl of my domain, and then I need to pass it as a javascript variable.
Unfortunately When the short url is generated, it causes an "Uncaught SyntaxError: Unexpected token ILLEGAL"
Here is my source code
<?php
function get_bitly_short_url($url,$login,$appkey,$format='txt')
{ $connectURL = 'http://api.j.mp/v3/shorten?login='.$login.'&apiKey='.$appkey.'&uri='.urlencode($url).'&format='.$format; return curl_get_result($connectURL);}
function curl_get_result($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$short_url = get_bitly_short_url('http://mydomain.com','BitLyUserName','ApiKey');
?>
<script>
var site = "<?php echo $short_url ?>";
var text = "Something else"
</script>
Please take a look and help me, I really need it

I believe you need to trim() the return data. I tested it out and curl was pushing in a newline character after the url, so the js was being output like:
var site = "http://j.mp/bMSmZV
";
Update the return from your function to be
return trim($data);
and you should be good to go.

Related

Using readability parser API to fetch image url from web page

Here I found readability which parse the data from web page and give information of interest.
But I could not understand how to use it.
https://www.readability.com/developers/api/parser
Request: GET /api/content/v1/parser?url=http://blog.readability.com/2011/02/step-up-be-heard-readability-ideas/&token=1b830931777ac7c2ac954e9f0d67df437175e66e
It gives json response.
I am working on PHP, how to make above request for particular url like http://google.com?
UPDATE1:
<?php
define('TOKEN', "1b830931777ac7c2ac954e9f0d67df437175e66e");
define('API_URL', "https://www.readability.com/api/content/v1/parser?url=%s&token=%s");
function get_image($url) {
// sanitize it so we don't break our api url
$encodedUrl = urlencode($url);
//$TOKEN = '1b830931777ac7c2ac954e9f0d67df437175e66e';
//Also tried with $API_URL = 'http://blog.readability.com/2011/02/step-up-be-heard-readability-ideas'; with no luck
// build our url
$url = sprintf(API_URL, $encodedUrl, TOKEN); //Also tried with $TOKEN
// call the api
$response = file_get_contents($url);
if( $response ) {
return false;
}
$json = json_decode($response);
if(!isset($json['lead_image_url'])) {
return false;
}
return $json['lead_image_url'];
}
echo get_image('http://nextbigwhat.com/');
?>
Any issue with code? gives error - >
Warning: file_get_contents(https://www.readability.com/api/content/v1/parser?url=http%3A%2F%2Fnextbigwhat.com&token=1b830931777ac7c2ac954e9f0d67df437175e66e): failed to open stream: HTTP request failed! HTTP/1.1 403 FORBIDDEN in F:\wamp\www\inviteold\test2.php on line 14
Perhaps something like this:
define('TOKEN', "your_token_here");
define('API_URL', "https://www.readability.com/api/content/v1/parser?url=%s&token=%s");
function get_image($url) {
// sanitize it so we don't break our api url
$encodedUrl = urlencode($url);
// build our url
$url = sprintf(API_URL, $encodedUrl, $token);
// call the api
$response = file_get_contents($url);
if( $response ) {
return false;
}
$json = json_decode($response);
if(!isset($json['lead_image_url'])) {
return false;
}
return $json['lead_image_url'];
}
The code is not tested so you might need to adjust it.

cURL get_data($url) with absolute URL

I'm using this code to get data using cURL
$url='http://example.com/'; //URL to get content from..
print_r(get_data($url)); //Dumps the content
/* Gets the data from a URL */
function get_data($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
However, This code returns data with relative url. How can I get ride of this relative url & print with absolute url? May be with preg_replace.. But How ?
Have a look at the HTML base tag. You should find it helpful if you want to let the browser do all the relative-to-absolute conversion:
$data = get_data($url);
// Note: ideally you should use DOM manipulation to inject the <base>
// tag inside the <head> section
$data = str_replace("<head>", "<head><base href=\"$url\">", $data);
echo $data;
I think that you must to use a HTML parser like http://simplehtmldom.sourceforge.net/, and replace all links with the correct path.

Google SafeBrowsing Lookup API - PHP

I'm trying to request information about a domain without success; code:
<?php
echo file_get_contents('https://sb-ssl.google.com/safebrowsing/api/lookup?client=asasd&apikey=MYKEY&appver=1.5.2&pver=3.0&url=http%3A%2F%2Fwww.onet.pl%2F');
?>
Why isn'tit working?
//function for getting the data from url
function get_data($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
Then get the content using the function call :
$returned_content = get_data('your url');
get_file_contents() has huge security threat – and many servers have disabled this feature in PHP.
Why isn'tit working?
because wrong url
http://www.google.com/safebrowsing/diagnostic?site=http://example.com/
just take a look at the documentation:
with URLs, you should use urlencode()
fopen wrapper has to be enabled (same as for fopen())
maybe the url is wrong - when i copy your URL and try to open it, i get a pageload-failure.

Using file_get_contents to different URLs in a loop seems to break the rest of the code

I am currently trying to fetch some facebook data, which I then want to access in Javascript. Specifically, I am trying to access some characteristics of the user's friends.
So I am getting the user's friend list using file_get_contents to his graph API URL.
This provides me with an array of friend ids.
As I need a characteristic from each friend, I am doing:
foreach($dataarray as $friend) {
$friendurl = "https://graph.facebook.com/".$friend->id."?access_token=".$token."";
$fdata = json_decode(file_get_contents($friendurl));
if($fdata->gender == "male") {
array_push($fulldata, $fdata->name);
}
}
Having this code piece seems to break the javascript code, as none of my alert instructions are ran.
Also, inserting a break after the if, so that only one file_get_contents is done, seems to make the code runnable (but I obviously need to go through all of the friends).
How can I solve this?
I would use jQuery or xmlHttpRequest to do the HTTP GET, but somehow I always seem to get back a status code of 0, with an empty response.
Edit:
Here is the JS code:
<script type="text/javascript">
function initialize() {
alert('Test1');
<?php
$fulldata = array();
$data = $result->data;
foreach($data as $friend) {
$friendurl = "https://graph.facebook.com/".$friend->id."?access_token=".$token."";
//echo("alert(\"".$friendurl."\");");
$fdata = json_decode(file_get_contents($friendurl));
if($fdata->hometown->name) {
array_push($fulldata, $fdata->hometown->name);
}
}
echo ("alert(\"".count($fulldata)."\")");
?>
}
</script>
I should've also added that this is being done on a page embedded into facebook using the canvas feature.
Try...
function curl($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
return curl_exec($ch);
curl_close ($ch);
}
foreach($dataarray as $friend){
$friendurl = "https://graph.facebook.com/".$friend->id."?access_token=".$token."";
$fdata = json_decode(curl($friendurl));
if($fdata->gender == "male"){
array_push($fulldata, $fdata->name);
}
}
Maybe FGC is disabled but you don't get any notifications/warnings.
Code from comment:
error_reporting(E_ALL); ini_set("display_errors", 1);
Note that you are doing cross-domain AJAX call which is prohibited for security reasons.
You can do the api call on the server and echo the data to the client side JS, or you can build a php proxy return the result of the Graph API call(As the proxy is at your own server, they are in the same domain).

Print out JSON with PHP

Nothing shows on the screen, is this valid code below? I know theres a JSON parameter called 'text' within the received data but not sure how to print it out?
<?php
$url='http://twitter.com/statuses/user_timeline/twostepmedia.json'; //rss link for the twitter timeline
//print_r(get_data($url)); //dumps the content, you can manipulate as you wish to
$obj = json_decode($data);
print $obj->{'text'};
/* gets the data from a URL */
function get_data($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
?>
This should work:
$obj = json_decode(get_data($url));
$text = $obj[0]->text;
It's a good idea to try something like var_dump($obj) when you run into an issue like this. After doing so, it becomes immediately clear $obj[0]->text is what you're after.
#benhowdle89 comment:
foreach ($obj as $item) {
$text = $item->text;
}
You should assign the value returned by get_data to a variable and pass it to json_decode i.e.:
<?php
$url='http://twitter.com/statuses/user_timeline/twostepmedia.json'; //rss link for the twitter timeline
//print_r(get_data($url)); //dumps the content, you can manipulate as you wish to
$data = get_data($url);
$obj = json_decode($data);
print $obj->text;
/* gets the data from a URL */
function get_data($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
?>
$data is not set, and you don't need the curly braces.

Categories