API url is being encoded with & instead of just & - php

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.

Related

Insightly API does not work

I am trying to connect to my Insightly account via API given on this link: https://github.com/Insightly/insightly-php
When I test the given code:
<?php
require('insightly.php');
function run_tests($apikey){
$insightly = new Insightly($apikey);
$insightly->test();
}
run_tests($argv[1]);
?>
Nothing actually happens, meaning it gives out a blank page. Of course I have changed the variable $apikey with the key given. I have tried with base64 encoding, single quotes, double quotes, but nothing really works. I have also tried on localhost as well as on my server.
Then I tried the code given on Github:
require("insightly.php");
$i = new Insightly('your-api-key');
$contacts = $i->getContacts();
Again, changing the api key with the one given, once normal and once in base64 encoding. This just gives me a 500 error.
Does anybody have any idea how to even connect to Insightly via API in PHP?
This code is made for executing in cli. If you use it like this is will run in a browser or anything else for that matter.
<?php
require('insightly.php');
function run_tests($apikey){
$insightly = new Insightly($apikey);
$insightly->test();
}
$apikey = "[PLACE API KEY HERE]";
run_tests($apikey);
otherwise try the following:
require("insightly.php");
$i = new Insightly('your-api-key');
$contacts = $i->getContacts();
var_dump($contacts);

Curl and codeigniter passing in POST data from an from into the url

Tying myself in knots a little bit with cURL. Using codeigniter for my framework and downloaded the CURL lib from getsparks.org/packages/curl .
I have this URL http://localhost:8984/rest?run=loc.xq&$a=York so this is a Rest for BaseX that I pass in a city (like York) and it shows me a carpark, This works in my browser. So what i wanted to do is have a user type the name into a form and pass that to basex and display the results. I have me view, controller set up, and this is currently the code I have in the controller
$this->load->library('curl');
$url = "http://localhost:8984/rest?run=loc.xq&$a=";
$datai = array($this->input->POST('carpark_location'));
$results = $this->curl->simple_get($url,$datai);
so this to me looks right, but its not, comes back with an error,
Message: Undefined variable: a
which I am guessing its picking $a out of the text as a variable, how would I make it ignore that?
not sure if just that will solve my issue but printing out the $url and $datai looks like below?
http://localhost:8984/rest?run=loc.xq&=Array ( [0] => York )
what am I missing?
Try this instead:
$url = 'http://localhost:8984/rest?run=loc.xq&$a='
Because it is inside single quotes instead of double quotes, PHP should treat $a as if it is part of the string instead of as a variable.

Shorten Link with php and API

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());

Codeigniter URI function url_title() how to use

I am using the CodeIgniter framework and I am confused on how to remove %20 from my urls. Below is an example of my code.
Controller - blog
method - show
attribute - this is my blog
public function show($blog= null)
{
// my attempt to set the uri segment
$blogName = $this->uri->segment(3, url_title($blog));
... //other code
}
this doesnt work, I am confused where I implement the url_title('this is my blog') function so that on page load it shows:
/blog/show/this-is-my-blog
do I need to do something in the config/routes.php file?
thank you!
EDIT:
Alright so I found out that url_title() output this20is20my20blog so I now have this:
$blogUrl = str_replace("%20", "-", $blog);
$this->uri->segment(3, $blogUrl);
but it still returns the URL with %20
You just need to use the native php function urldecode to remove any characters that get encoded in a url. Spaces in a URL get encoded to %20 so instead of doing a str_replace just try
public function show($blog= null)
{
// i'm not sure what url_title does so you might have to tweak this a little
$blogName = $this->uri->segment(3, urldecode(url_title($blog)));
}
http://php.net/manual/en/function.urldecode.php
Try echoing the url_title() just before you do the $this->uri->segment(); to ensure it's returning correctly.

How to create a short url for sharing on social media

I'm trying to write some code which shares a page on Facebook and twitter.
The problem I'm facing is that the page I'm trying to share has a big query string like:
http://domain.com/see.php?c=3&a=123&v=1
But it seems that Facebook and Twitter don't like that big query string.
I also tried using tiny url with following method in which I passed the URL to a PHP function to get the tiny URL:
var a = $("#Link").val();
I get the correct value of **a**. After that I pass this value to a PHP file:
$.post("ShortLink.php?value="+a
In that PHP file I got the following value:
http://domain.com/see.php?c=3
All the values after 3 is deleted.
Thanks
When POSTing to your ShortLink.php file, you should make sure to URL encode the value of a beforehand. Otherwise you're calling ShortLink.php?value=http://domain.com/see.php?c=3&a=123&v=1, i.e., sending:
value = http://domain.com/see.php?c=3
a = 123
v = 1
What you want is ShortLink.php?value=http%3A%2F%2Fdomain.com%2Fsee.php%3Fc%3D3%26a%3D123%26v%3D1, thus sending:
value = http://domain.com/see.php?c=3&a=123&v=1
This can be achieved via encodeURIComponent():
$.post("ShortLink.php?value=" + encodeURIComponent(a));
See also How do I pass a URL with multiple parameters into a URL? and How to encode a URL in Javascript?.
Why don't u just use an url shortener API for that, like Google url shortener. That way, you can leave your code the way it is, but for site's like Facebook and Twitter, it is nicely short.
Try this:
$.post("ShortLink.php?value=" + escape(a));

Categories