PHP get url with special characters without urlencode:ing them! - php

I would like file_get_contents to get a url that looks like this: http://wapedia.mobi/sv/Gröt
The problem is that it requests (can't post entire link, sorry): ...wapedia.mobi/sv/Gr%C3%B6t which you can see has been urlencoded, that page does not give me any results.
How can I do this?

According to the PHP manual, you must specifically encode a URL if it contains special characters. This means the function itself should do no special encoding. Most likely your URL is being encoded before being passed to the function, so pass it through urldecode first and see what happens.
Edit: You're saying the encoding is being messed up. Again the PHP manual specifically states that you need to encode urls prior to passing them to file_get_contents. Try encoding the URL, then passing it to the function.
$url = urlencode($url);
file_get_contents($url);

Related

PHP file_get_contents() Chinese character ERROR CODE

I use file_get_contents() to download a JSON. There're some Chinese characters in the URL, I tried to print the URL out, it's OK. But when I ran the program, the URL I put in the function became error code. How do I know that is this URL links to a JSON that links to a MySQL request, and in the console of MySQL, I saw the URL became error code. I tried lots of ways to change URL string to UTF-8 or GB2312, etc, but none of that works. I Wish I could get help here, thanks.
Its very difficult to understand your question. I think i understood the first part of your question:
I use file_get_contents() to download a JSON. There're some Chinese
characters in the URL, I tried to print the URL out, it's OK. But when
I ran the program, the URL I put in the function became error code.
You try to access a URL containing chinese characters using file_get_contents():
The answer to this is:
You need to encode the part of the url containing chinese characters using urlencode() or rawurlencode().
The main difference between urlencode()and rawurlencode() is, that urlencode() converts spaces to +. rawurlencode() converts spaces to %20.
urlencode is used for Query Parameters as example ?q=my+search+key, in every other case you use rawurlencode.
Example:
$test = 'http://www.example.com/'.rawurlencode('以怎么下载').'.html';
print_r($test);
// $html = file_get_contents($test);
// output:
http://www.example.com/%E4%BB%A5%E6%80%8E%E4%B9%88%E4%B8%8B%E8%BD%BD.html
I hope it solves your problem.

Angled brackets being escaped from url in PHP

I am trying to call the USPS API that takes in the zip code and returns XML containing the City Name of the given zip code.
Here is the URL they require:
http://production.shippingapis.com/ShippingAPITest.dll?API=CityStateLookup
&XML=<CityStateLookupRequest USERID="xxxxxxx"><ZipCode ID= "0">
<Zip5>90210</Zip5></ZipCode></CityStateLookupRequest>
In my PHP file, when I echo out the above URL, this is what I get:
http://production.shippingapis.com/ShippingAPITest.dll?API=CityStateLookup&XML=90007
All the XML part of the URL is missing. I need to get curl data from the URL.
Anyone know what I could be missing?
Anyone know what I could be missing?
Probably. Maybe, yes. What you describe in your posting sounds like an encoding problem. So you are missing the right encoding.
As you are talking about an URL that is likely URL encoding. Some characters - like space - have a special meaning inside an URL so you can not just use any character as you like, but you need to encode all characters properly.
The exact description how you need to formulate an URL incl. the exact description how URL encoding works is outlined in 2. Characters in the internet standard RFC3986.
PHP functions related to URL encoding are urlencode() and rawurlencode and more likely useful in your case http_build_query().
http://php.net/manual/en/function.urlencode.php should get you started if you want to encode the xml into the URL.

Fetch contents from a unicode URL

I would like to fetch the contents (HTML) from this URL: http://www.tvsporedi.si/spored.php?id=Vaš kanal. I've tried with File_Get_Contents and cURL.
No matter how I construct the URL in my code the page always returns a blank page (a page with header&menu but no content). I tried url encoding the id parameter, leaving the id as it is, without any luck. The only change I can make to the url (it seems) is encoding the space (with %20. Encoding the š does not work.
So I guess what I'm asking is why does PHP "eat" the š? The PHP file is saved with UTF-8 encoding ...
Try fetching:
http://www.tvsporedi.si/spored.php?id=Va%C5%A1%20kanal
This works for me in a browser, and I would guess with whatever method you use.
I used Firebug to see how the browser was encoding the request...
works for me with urlencode:
readfile('http://www.tvsporedi.si/spored.php?id=' . urlencode('Vaš kanal'));
demo

URL as URL's get parameters - problem with "&"

There is script that receives another url as GET parameter:
script.php?file=http://www.google.com&id=123
The problem is:
when url has parameter itself, it is used as script's parameter, not that url's parameter:
script.php?file=http://www.google.com?q=adsf&lang=en&id=123
URL is http://www.google.com?q=adsf&lang=en, but it is chopped after &, because it is viewed as related to script.php itself.
What can I do about this?
I tired to replace & with %26, but url get broken with it.
You need to encode the value with the percent-encoding.
If you’re using PHP, use rawurlencode (or urlencode if application/x-www-form-urlencoded is expected):
$url = 'http://www.google.com?q=adsf&lang=en';
echo 'script.php?file='.rawurlencode($url);
You need to URL encode the entire URL that you are passing as a parameter to another url (your script). %26 is the correct encoding for an &. Just make sure you decode it server-side before using it. You don't say what language(s) you're using, but most, inc javascript and php have native URL encoding functions.
Try to encode every special character like this:
script.php?file=http%3a%2f%2fwww.google.com%3fq%3dadsf%26lang%3den&id=123
although it might be better and easier to use rawurlencode().
Also, read this about URL encoding.

Pass text with special characters as get parameter in php

I want to pass any text as a get parameter to a php script. For know I just append the text this way:
action.php?text=Hello+my+name+is+bob
This url is composed by javascript and I do a ajax request with this url.
In action.php I do
$encoded = array_map('rawurlencode', $_GET);
But this does not work for special chars like ÖÄüä.
Any idea how to solve this?
url_encode(string) will return the given string with special characters converted into %XX format.
http://us3.php.net/manual/en/function.urlencode.php
I know you can send special characters fine without encoding through $_POST, which is another alternative
try with url_encode().........

Categories