How to echo-ing decoded url from GET method on php,
i have this link:
localhost/whatsapp?text=Hello%2C%20Who%20are%20U%3F%0AName%3A%20%0AAddress%3A%20%0AAge%3A%20
with this code
$text = $_GET['text'];
header("Location: https://web.whatsapp.com/send?phone=00000000&text=".$text);
but get error
Warning: Header may not contain more than a single header, new line detected in index.php on line
When you retrieve values from $_GET, they are URL decoded. If you want to pass it back, you need to encode it again, so that newlines, spaces and other weird characters gets encoded. Use urlencode($text) for this.
$text = $_GET['text'];
header("Location: https://web.whatsapp.com/send?phone=00000000&text=".urlencode($text));
You will have to parse the incoming encoded URL before adding the correct part to the new URL, such as:
<?php
$url = 'localhost/wa-gen/?text=Hello%2C%20Who%20are%20U%3F%0AName%3A%20%0AAddress%3A%20%0AAge%3A%20';
$newURL = "https://web.whatsapp.com/send?phone=00000000&".parse_url($url, PHP_URL_QUERY);
?>
Parse_url is very good at extracting pieces from a given URL.
Related
I have a URL with space characters that I want to properly encode when making an API call (in this case replace ' ' with '%20' so the space characters are treated properly.
$url = 'https://www.someapi.com?param=this and that';
Hoever when using the urlencode($url) function I get this obscure representation of the URL
"https%3A%2F%2..."
That I eventually cannot resolve.
........
Is there a URL-encode function in PHP that just replaces spaces and quotation marks instead of making abrupt changes to the whole string?
You can parse request url and only encode query string "values".
$url = "https://www.someapi.com?param=this and that";
$baseUrl = explode("?",$url)[0];
parse_str(parse_url($url,PHP_URL_QUERY),$args);
foreach ($args as $key=>&$arg){
$arg= urlencode($arg);
}
echo $baseUrl."?".http_build_query($args);
Result:
https://www.someapi.com?param=this%2Band%2Bthat
I have a long URL:
$url='http://www.likecool.com/Car/Motorcycle/BMW%20Urban%20Racer%20Concept%20Motorcycle/BMW-Urban-Racer-Concept-Motorcycle.jpg';
I create a short one:
$url='http://goo.gl/oZ04P8';
$url='http://bit.ly/1CzOQbf';
I run $headers = get_headers($url); print_r($headers);
SCENARIO:
get_headers works correctly for goo.gl short code but incorrectly for BITLY shortcode (404).
The difference is that BITLY shows up with spaces in the long url (bad) and GOOGL %20 (good).
When get_headers redirects the (long) url (with spaces) it FAILS.
I see no obvious way to fix this - am I missing something?
TWO OPTIONS
- change the way BITLY encodes ? (I force %20 formatting in long url)
- change the way get_headers encodes its URLs
You could replace the content of the header by yourself once you received it :
$url = 'http://bit.ly/1CzOQbf';
$headers = get_headers($url, 1);
$headers['Location'] = str_replace(' ', '%20', $headers['Location']);
print_r($headers);
Output :
[Location]=>http://www.likecool.com/Car/Motorcycle/BMW%20Urban%20Racer%20Concept%20Motorcycle/BMW-Urban-Racer-Concept-Motorcycle-1.jpg
I added the second parameter of get_headers so it names the keys of the returned array, that way it's clearer to use / modify. It is obviously not needed at all.
I want to get a a URL content using file_get_contents($url); when I copy the URL from browser address bar it is like this:
$url="http://www.mashadhome.com/fa-estate-39855-tags-%D9%81%D8%B1%D9%88%D8%B4-%D8%A2%D9%BE%D8%A7%D8%B1%D8%AA%D9%85%D8%A7%D9%86-%D8%A8%D9%84%D9%88%D8%A7%D8%B1%20%D8%B5%DB%8C%D8%A7%D8%AF%20%D8%B4%DB%8C%D8%B1%D8%A7%D8%B2%DB%8C";
but when I automatic get the url using
$homepage1 = file_get_contents($url_value);
$doc1 = new DOMDocument;
$doc1->preserveWhiteSpace = false;
#$doc1->loadHTML($homepage1);
$xpath1 = new DOMXpath($doc1);
$nodes1 = $xpath1->query("//html/body/section/div/div/section/figure/a");
$href = $node1->getAttribute('href');
it is sothing like this:
$href="http://www.mashadhome.com/fa-estate-39855-tags-فروش-آپارتمان-بلوار صیاد شیرازی";
I use code like above to get content of this link, but the file_get_contents($href) don't work for second URL, either when I copy second address to browser it works good;
so question is this: why second address doesn't work? how to convert first address to second type?
Url can accept restricted character set, namely ASCII letter, digits, hyphen. To access such url, it needs to be encoded to the format accepted by your server, like in your first example. Have a look at urlencode() function.
Of course you need to use urlencode only on parts that are not url special characters (like :, /). In this instance, you would use urlencode on the fa-estate-39855-tags-فروش-آپارتمان-بلوار صیاد شیرازی part only.
I have a url request like this:
http://localhost/pro/api/index/update_profile?data={"id":"51","name":"abc","address":"stffu fsagu asfhgui fsahgiu3#$#^^##%^3 6\"\"wkgforqf\";rqgjrg..,,,rqwgtr''qwrgtrw'trwqt'rqwtqwr trqt\n"}
I am trying to json decode of this url.I use following code to decode url.It is working perfect if url not contain special character. but how to decode it if it contains special character.
$string = htmlspecialchars($_REQUEST['data'], ENT_QUOTES);
$jsonFix = urldecode($string);
$string = htmlentities($jsonFix, ENT_QUOTES | ENT_IGNORE, "UTF-8");
$json = json_decode($string, true);
print_r($json);exit;
I tried this code but it is not working.when i am try following:
print_r($_REQUEST['data']);exit;
output is:
{"id":"51","name":"ds"","address":"stffu fsagu asfhgui fsahgiu3
means it is bracking from # character.
(sidenote: i am working on api for iphone so request came from iphone,framework:CI)
so how to get url which contain special character and how to decode it?
The # character marks the beginning of the fragment part of the URL.
You need to properly URL-encode the URL for this to work.
For example, your JSON, when correctly URL-encoded, becomes:
%7B%22id%22%3A%2251%22%2C%22name%22%3A%22abc%22%2C%22address%22%3A%22stffu%20fsagu%20asfhgui%20fsahgiu3%23%24%40%5E%5E%40%23%25%5E3%206%5C%22%5C%22wkgforqf%5C%22%3Brqgjrg..%2C%2C%2Crqwgtr%27%27qwrgtrw%27trwqt%27rqwtqwr%20trqt%5Cn%22%7D
The entire URL becomes:
http://localhost/pro/api/index/update_profile?data=%7B%22id%22%3A%2251%22%2C%22name%22%3A%22abc%22%2C%22address%22%3A%22stffu%20fsagu%20asfhgui%20fsahgiu3%23%24%40%5E%5E%40%23%25%5E3%206%5C%22%5C%22wkgforqf%5C%22%3Brqgjrg..%2C%2C%2Crqwgtr%27%27qwrgtrw%27trwqt%27rqwtqwr%20trqt%5Cn%22%7D
Check the documentation of your language of choice to find the correct method for URL-encoding characters.
For example, in PHP, this is rawurlencode and in JavaScript this is encodeURIComponent.
If necessary, there are also plenty of URL coders online, such as this website.
You are manipulating the $data in some ways that aren't really necessary. htmlspecialchars() and htmlentities() make sense if applied to specific values - not the whole JSON. The danger is that they mess up the JSON, it is only important here to urldecode()!
$jsonFix = urldecode($data);
$json = json_decode($jsonFix, true);
This already works and doesn't leave any character out.
If you plan to post something of that and want to escape it, you can do it like so
htmlspecialchars($json['address'], ENT_QUOTES)
Can't you just replace the "#" character with something like "&hashtagChar;" before you process, and put it back afterwards?
I am using the following code to encode a URL for basic hiding of the URL
/lbs_map.php?msisdn=27827910118
This is what I do not want my clients to see. I have coded it the following way
<a href="lbs_map.php?msisdn=<?php echo base64_encode ("27".substr($rows['member_msisdn'],
1)); ?>
This is my output now:
/lbs_map.php?msisdn=Mjc4Mjc5MTAxMTk=
I am using this to try and decode the string:
<?php
$str = 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==';
echo base64_decode($str);
?>
But it is not working at all to decode it and give me the required info i want. I need help on the decoding of the string
The encode string must work with the code string as the code string varies and is never the same
If you're passing base64 encoded data via the url, you need to urlencode() it first as = is a reserved character in urls.
You need to urlencode() the msisdn parameter.
Also keep in mind that base64 is not the way to go if you want to hide something from your users as it's not an encryption function and can be easily decoded.