Shorten Link with php and API - php

I already have a function on my page that produces url from curl. I need to use the following string to return short url in text format or "simple" using the shortswitch.com api.
Here Is my code:
<?php
$long_url = urlencode('curPageURL()');
$url = "http://api.shortswitch.com/shorten?apiKey=[apikey]&format=simple&longUrl={$long_url}";
$result = file_get_contents($url);
print_r($result);
?>
I'm attempting to use a function I found for bit.ly to use with shortswitch.com api as shortswitch.com/admin/api.
My problem is that I'm not getting any type of output from the function, no shortened url is being generated.
best regards,

I'll just guess that you're actually trying to call the function curPageURL() and url encode its result instead of url encoding the string "curPageURL()":
$long_url = urlencode(curPageURL());

Related

API url is being encoded with & instead of just &

I'm trying to connect my app up to an API using this code:
class PostcodePrice{
public function getPrice($postcodes){
$apikey = "MYAPIKEY";
$priceurl = "http://api.zoopla.co.uk/api/v1/area_value_graphs.js?area=".$postcodes."&output_type=outcode&api_key=".$apikey;
$price = file_get_contents($priceurl);
$decoded = json_decode($price);
return $decoded->result;
}
}
Now the problem I'm having is my url is being passed to the api and replacing my & with & so my url is looking like this:
http://api.zoopla.co.uk/api/v1/area_value_graphs.js?area="postcode&output_type=outcode&api_key=MYAPIKEY
The API I am using refuses the url with the encoded & so it obviously doesn't work as I get 403's back as a response.
I've tried things such as string replace, htmlspecialchars etc.
Anyone know of how I can stop this from happening?
U can make "$priceurl" url without concatenation like this.
$priceurl = "http://api.zoopla.co.uk/api/v1/area_value_graphs.js?area=$postcodes&output_type=outcode&api_key=$apikey";
Hope it's work for u.

Saving Google API response in php variable

I would like to call a php function which will process a google API call and save particular variable so I can work with it.
I know below code won´t work but I hope it will give you an idea what I want to do.
Thank you in advance for your advise.
function callGoogleAPI () {
// calling the fGoggle spi somehow
https://maps.googleapis.com/maps/api/timezone/json?location=45.908133,-77.047119&timestamp=1472106746
$dstOffset = dstOffset;
}
When the function is executed I should have value "3600" saved in my dstOffset variable.
Thank you
Vinny
It's very basic question. You can call api via file_get_contents() or via curl then decode json with json_decode function.
function callGoogleAPI () {
// calling the fGoggle spi somehow
$content = file_get_contents('https://maps.googleapis.com/maps/api/timezone/json?location=45.908133,-77.047119&timestamp=1472106746');
$content = json_decode($content, true);
$dstOffset = $content['dstOffset'];
}
Using REST APIs in PHP is a three step process.
1) You cal the API and store the returned data in a variable
$maps_content = file_get_contents('https://maps.googleapis.com/maps/api/timezone/json?location=45.908133,-77.047119&timestamp=1472106746');
2) You convert the JSON object in an associative array
$maps_array = json_decode($content, true);
3) You start working with the data in your array
$dstOffset = $content['dstOffset'];
To see what your variable looks like, don't forget to var_dump
var_dump($maps_array);

How to get proper Twitter share count for a URL?

I am implementing a function to get Twitter count for my URL in PHP. I am trying to get count using:
$json_string = file_get_contents("http://urls.api.twitter.com/1/urls/count.json?url=http://themarketmogul.com/will-uber-ipo-in-2015");
$json = json_decode($json_string, true);
echo intval( $json['count'] );
This return 5 count but when I directly hit this URL in the browser then I get absolute count 6.
Please help me to find found proper count in PHP?
The URL you are using is deprecated and won't work anymore. Try http://opensharecount.com to get a URL that will work.

How do I strip the url of a google news json feed link

How do I strip the url of a google news json feed link?
My link looks like this
http%253A%252F%252Fwww.boston.com%252Fbusiness%252Fnews%252F2014%252F12%252F04%252Fnasa-poised-usher-new-era-with-orion-launch%252FGOh9asOZiRJPHbNw60otUK%252Fstory.html
I have used the following code
strip_tags("".$row['url']."");
This does not do anything, I'm guessing this is possible and I am using the wrong php function.
You can use urldecode for this.
$url = "http%253A%252F%252Fwww.boston.com%252Fbusiness%252Fnews%252F2014%252F12%252F04%252Fnasa-poised-usher-new-era-with-orion-launch%252FGOh9asOZiRJPHbNw60otUK%252Fstory.html";
$url = urldecode(urldecode($url));
echo $url; // Will output: http://www.boston.com/business/news/2014/12/04/nasa-poised-usher-new-era-with-orion-launch/GOh9asOZiRJPHbNw60otUK/story.html

picasa link to usable cooliris link

Ok, I've got this link:
https://picasaweb.google.com/110727246651632161982/2042011#
and I want to use it for cooliris, on their website they make it like this:
picasaweb.google.com%2F%3Fuser%3D110727246651632161982%26album%3D2042011
I want users to post the normal picasa link into my website, then the link has to be replaced and pasted into my database. I know how to do that, but how to get from the picasa link to the usable cooliris link?
Example: DECODE
$url = 'picasaweb.google.com%2F%3Fuser%3D110727246651632161982%26album%3D2042011';
echo urldecode($url);
Output
picasaweb.google.com/?user=110727246651632161982&album=2042011
Example: ENCODE
$url = 'picasaweb.google.com/110727246651632161982/2042011#';
echo urlencode($url);
Output
picasaweb.google.com%2F110727246651632161982%2F2042011%23
References:
urldecode()
urlencode()

Categories