Encoding params of url that generated dynamically for use in CURL - php

I write a robot and fetch all URL in web page. the URLs is be like this:
http://www.example.com/result/رایانه
Now, When I try to fetch content of this url by CURL, give me this error:
400 Bad request
I know this cause because for "رایانه" in url and must encoding it.
But that URL is dynamically, and I need a solution for encode just params in URL.
Be like this:
"http://www.example.com/result/" . urlencode("رایانه")
Or another example:
Maybe I have this URL:
http://www.example.com/result/سوتی/?foo=علی&bar=حسن
If I use urlencode() return this:
http%3A%2F%2Fwww.example.com%2Fresult%2F%D8%B3%D9%88%D8%AA%DB%8C%2F%3Ffoo%3D%D8%B9%D9%84%DB%8C%26bar%3D%D8%AD%D8%B3%D9%86
But so must just encode this words: سوتی, علی, حسن.
and have this correct encoded:
http://www.example.com/result/%D8%B3%D9%88%D8%AA%DB%8C/?foo=%D8%B9%D9%84%DB%8C&bar=%D8%AD%D8%B3%D9%86
I need this for use in CURL.
How can I do this?
EDIT:
I found this code:
echo implode("/", array_map("urlencode", explode("/", $string)));
return:
http%3A//www.example.com/result/%D8%B3%D9%88%D8%AA%DB%8C/%3Ffoo%3D%D8%B9%D9%84%DB%8C%26bar%3D%D8%AD%D8%B3%D9%86
But the result is not exactly true.

I find a solution:
$string = 'http://www.example.com/result/سوتی/?foo=علی&bar=حسن';
$string = urlencode($string);
echo str_replace(array('%3A', '%2F', '%3F', '%3D', '%26'), array(':', '/', '?', '=', '&'), $string);
Output:
http://www.example.com/result/%D8%B3%D9%88%D8%AA%DB%8C/?foo=%D8%B9%D9%84%DB%8C&bar=%D8%AD%D8%B3%D9%86
It work with any URL and accept for use in CURL.

Related

How to convert this url to %20?

A very simple problem which might have been solved here a lot of time, but I'm not getting what I want.
I have an image url like this:
$image = 'http://perfumepalace.arctechsolution.com/image/cache/data/another/For Her/Believe 100ml EDP-150x150.jpg';
and want output in this form
http://perfumepalace.arctechsolution.com/image/cache/data/another/For%20Her/Believe%20100ml%20EDP-150x150.jpg
Yes ofcourse you will tell me I can use urlencode or rawurlencode. Believe me I've tried and still no luck.
With urlencode I get like this
http%3A%2F%2Fperfumepalace.arctechsolution.com%2Fimage%2Fcache%2Fdata%2Fanother%2FFor+Her%2FBelieve+100ml+EDP-150x150.jpg
And with rawurlencode I received output like this:
http%3A%2F%2Fperfumepalace.arctechsolution.com%2Fimage%2Fcache%2Fdata%2Fanother%2FFor%20Her%2FBelieve%20100ml%20EDP-150x150.jpg
With str_replace I can get exactly what I want like this:
str_replace('+', '%20', $image);
will result: http://perfumepalace.arctechsolution.com/image/cache/data/another/For%20Her/Believe%20100ml%20EDP-150x150.jpg
but I want to get them using urlencode or rawurlencode. But these functions encode even slash / part as well.
Is there any way to url encode only spaces in the url?
Edit: Yes i can use basename() or pathinfo to encode only part of the url. But the directory name might also contain the space character, so converting only filename is also not a possibility here.
And actually I want to know if we can use urlencode, or rawurlencode in full url without affecting the '/' of the fullurl. I don't want the regex suggestions eigher.
Note: The complication part is that 'For Her' directory section in the url path, and the level of directory is also not fixed.
What you need is rawurlencode(), but only encode the necessary part of your url.
Example:
$image = sprintf(
'http://perfumepalace.arctechsolution.com/image/cache/data/another/%s/%s',
rawurlencode('For Her'),
rawurlencode('Believe 100ml EDP-150x150.jpg')
);
But if you still want to apply to the whole url string, you could do like below:
function my_url_encode($url) {
$info = parse_url($url);
return sprintf('%s://%s/%s',
$info['scheme'],
$info['host'],
implode('/', array_map('rawurlencode', explode('/', $info['path']))));
}
UrlEncode should be used only when encoding a string to be used in query of the url.
So my suggestion here is for you to keep with the str_replace('+', '%20', $image); code, as you have the full url and not only the query part.
The other option is to parse the url, extract the path, encode it and rebuild the url, here is one example:
$image = 'http://perfumepalace.arctechsolution.com/image/cache/data/another/For Her/Believe 100ml EDP-150x150.jpg'
$path = parse_url($image, PHP_URL_PATH);//extract the path
$epath = rawurlencode($path);//encode it
$enc_image = 'http://perfumepalace.arctechsolution.com/'.$epath;//build the new url
Try this:
$image = 'http://perfumepalace.arctechsolution.com/image/cache/data/another/For Her/Believe 100ml EDP-150x150.jpg';
$parts = parse_url($image);
$image_path = explode('/',$parts['path']);
foreach($image_path as $key => $image_path_part){
$image_path[$key] = rawurlencode($image_path_part);
}
echo $parts['scheme']."://".$parts['host'].implode('/',$image_path);
Output: http://perfumepalace.arctechsolution.com/image/cache/data/another/For%20Her/Believe%20100ml%20EDP-150x150.jpg
Have a nice day!!

PHP - Get the end of a URL

So I think I need to submit a new question for this...
Here is my old question: PHP - Get path minus root
I need a way in PHP to take the URL being any of the following...
http://kenthomes.net/plan_detail.php?mod=39
http://kenthomes.net/Amelia-Cove
and get everything after leaving me with...
"plan_detail.php?mod=39" // If there is no alias for that page
OR
"Amelia-Cove" // If that page has an alias being applied
In reality, they are the same page, because of the alias, but not all of these pages have aliases associated with them such as...
http://kenthomes.net/plan_detail.php?mod=52
unlike...
http://kenthomes.net/Amelia-Cove
Currently I am using...
trim($_SERVER['REQUEST_URI'],'/')
which gives me...
"Amelia-Cove" // Which is fine.
OR
"plan_detail.php" // Which is not okay.
I need..
"Amelia-Cove" // Which is fine.
OR
"plan_detail.php?mod=39" // Which is fine.
How do I do this?
You can get all the parts of an URL via parse_url()
For example;
$parts = parse_url('http://kenthomes.net/plan_detail.php?mod=39');
print_r($parts);
Should give you something like this:
Array
(
[scheme] => http
[host] => kenthomes.net
[path] => /plan_detail.php
[query] => mod=39
)
Which you can use to create your own URL containing the parts that you need
$_SERVER["REQUEST_URI"] only contains the URI.
When you also want the part after the ?, you need to also use $_SERVER["QUERY_STRING"].
Use:
trim($_SERVER['REQUEST_URI'] . $_SERVER['QUERY_STRING'], '/');
Append $_SERVER['REQUEST_URI'] with $_SERVER['QUERY_STRING'].
PHP: $_SERVER - Manual
You can get the query string (the bit after the question mark), via $_SERVER['QUERY_STRING']
parse_url, and if you just want the far right, using str_split would be sufficient.
$data = parse_url($url, PHP_URL_PATH);
Should be enough.
Otherwise if responding to the current request, $_SERVER['REQUEST_URI'] might work, as that is the entire URI.
You can try this:
$uri = $_SERVER['REQUEST_URI'];
$qs = $_SERVER['QUERY_STRING'];
echo trim($uri . $qs, '/');

Pass a full url with Params in PHP

I want to pass a url as a parameter in my PHP script. The URL is coming from my Android app which passes a url and the PHP script custom formats XML to return to the app. So I would pass the url I wanted to format and the PHP script would do stuff and echo the results.
For instance I want to pass this url:
differentwebpage.com/search/?query=ford%20f150&
in my php script (mywebpage.com/script.php):
$url = $_GET['url'];
$str = file_get_contents($url);
echo $str;
So the full url would be:
mywebpage.com/script.php?url=differentwebpage.com/search/?query=ford%20f150&
But this doesn't work. I think I need to URL encode/decode but I haven't had any success. How can I pass a full URL as a parameter?
Try encoding it in Android when you pass it.
You can encode using the URLEncoder here: http://developer.android.com/reference/java/net/URLEncoder.html
You can then easily unencode it in PHP using urldecode.
$url = urldecode($_GET['url']);
$str = file_get_contents($url);
echo $str;
Simply use the php function urlencode(). It will encode the URL in your example like this:
differentwebpage.com%2Fsearch%2F%3Fquery%3Dford%2520f150%26
Which can be fully decoded. And only contains characters that are valid in a URL.

Encoding $_GET variable URL ignores ampersand "&" character

So im trying to get an url from the address bar that looks like this:
http://mysite.com/url.php?name=http://test.com/format.jsp?id=738ths3&secure=false
I'm using the $_GET variable to read it right off the URL my code is as follows
$arc = rawurlencode($_GET['name']);
echo "URL: $arc";
This only returns
URL: http://imgur.com/format.jsp?id=738ths3
It 's missing the &secure=false
What i want it to look:
URL: http://test.com/format.jsp?id=738ths3&secure=false
I have tried urlencode, rawurlencode with no avail, i have looked in google a number of forums and stackoverflow none of the answer help, any ideas? Thanks!
urlencode shows this:
URL: http%3A%2F%2Ftest.com
so i cant have that either!
You'll need to urlencode() before constructing the URL, ie:
$url = "http://mysite.com/url.php?name=".urlencode('http://test.com/format.jsp?id=738ths3&secure=false');
This way, you will be able obtain the full URL as a name GET parameter from $_GET['name'].
Explanation:
Without urlencode() it when constructing the URL, PHP would treat is as 2 separate parameters, separated by &:
$_GET['name']
which is http://imgur.com/format.jsp?id=738ths3 for your case
$_GET['secure']
which is false for your case
Alternatively:
From your comment, it seems that you do not have control for the URL construction. You can get the full $_GET in a single string using http_build_query:
$name = http_build_query($_GET);
You would then obtain:
echo $name; // name=http://test.com/format.jsp?id=738ths3&secure=false
// which you would then may want to strip away the first 'name='
$name = substr($name, strlen('name='));
echo $name; // to obtain http://test.com/format.jsp?id=738ths3&secure=false
The original URL, http://mysite.com/url.php?name=http://test.com/format.jsp?id=738ths3&secure=false, contains two query-string parameters: name and secure. The & in the query-string belongs to the full URL, not the URL in the name parameter.
If you have control over this value, when declaring the link/URL, use PHP's urlencode() to encode the full name value, such as:
$url = "http://mysite.com/url.php?name=" . urlencode("=http://test.com/format.jsp?id=738ths3&secure=false");
This will properly encode the name parameter and your $arc = $_GET['name']; will work as desired.
If you do not have control over setting the value and are simply parsing something you're receiving, you can split the given string on the name= parameter and assume everything else after it is part of name:
$splitQuery = split('name=', $_SERVER['REQUEST_URI']);
$arc = $splitQuery[1];
To decode the encoded URL, after you've accessed it, use PHP's urldecode():
$arc = urldecode($_GET['name']); // assuming you're properly encoding the `name` parameter
If you cannot encode the URL, you can get the current URI with this code:
$url = $_SERVER["REQUEST_URI"];
that in your case, the $url is :
/url.php?name=http://test.com/format.jsp?id=738ths3&secure=false
Then you can split it with explode and validate it and take the GET params from it.

parse_url() problem when one of parameters is url

I call a php script http://site.com/process.php that takes a url as one of its parameters. for=
http://site.com/process.php?for=http://www.anotherwebsite.com
I then do this and try to parse_url() but parse_url() gives a parse error.
$uri = $_SERVER['REQUEST_URI']; // /process.php?for=http://www.anotherwebsite.com
parse_url($uri);
How can I encode the for parameter either on the sending side (in the url) or on the receiving side (php) so that parse_url() understands that it's just a parameter that happens to look like a url?
Before including your url as a get parameter, use urlencode
$full_url = 'http://site.com/process.php?for=' . urlencode('http://www.anotherwebsite.com');
This function is convenient when
encoding a string to be used in a
query part of a URL, as a convenient
way to pass variables to the next
page.
To reverse the result of urlencode, use urldecode. As mario pointed out in a comment below, $_GET parameters are already urldecoded.
Well, first you must urlencode() the for= parameter, then in process.php, you can simply do
$url = $_GET["for"];
$url = urldecode($url); // http://www.anotherwebsite.com
Here are the functions:
http://php.net/manual/en/function.urlencode.php
http://php.net/manual/en/function.urldecode.php

Categories