How to send percentage in url? - php

I am trying to send a url like this for search data
http://localhost/project/search/text:75%
I am getting 400 - Bad Request error in here.
I even tried replacing percentage with %25. But it didn't worked. How should I send the search data containing percentage?

In URLs, the % percent character is reserved for character encoding.
Usually to represent a % character you can use %25, but as you have already tried this and that it doesn't work for you, you should instead use PHP's urlencode function like so:
$url=urlencode("text:%75");
The same issue occurs with :, this therefore prevents the same issue with this character also (which for reference is %3A).
Partially from this question.

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.

Strange URL encoding characters at the end of CSRF Tokens Laravel Sanctum

I'm trying to learn how to use Laravel Sanctum authentication. When I send GET https://localhost/sanctum/csrf-cookie I get the following CSRF cookies:
XSRF-TOKEN=eyJpdiI6Inhvb0FDVXdHZDU5QzBqQTNKaWNxTUE9PSIsInZhbHVlIjoiSXNudjNiNE9xbmtNVWdsQ0l2SDRyYUNPQXIrTGJLb2ZMVDc2NWttenZGY0NkcDRvQzFVQlZOMDRlNFdTOHJaNiIsIm1hYyI6ImY0Y2M2YzZiZWIxYWVmZTRmMWI5NWRhNTBhN2JmM2VjNGExYjU0MGYwYWVmYTE4ODQxM2I0YTFlMWVjZTVhMDkifQ%3D%3D;
You can notice the strange %3D%3D at the end of the token. These characters also added for my laravel_session cookie.
When I then send back a request with this exact token in the header X-XSRF-TOKEN, I'm getting token mismatch error. When I remove the characters - all works. I wonder where's these characters came from and how can I remove them.
UPD: since those were encoded URL characters, when I decoded them and put '==' instead at the end of X-XSRF-TOKEN, that seems to be working. Still, it's strange why it worked before when I just removed the characters from the query manually.
Yeah this stands for the = symbol which is part of your base64'ed CSRF token. I'd guess it only works when you remove it because the = symbol is the special padding character. In a very high level they just pad the string out to the proper length.
As you already know, = becomes %3D when it is url encoded.
Usually when you encounter a string with lot of numbers and characters in random order and it ends with == there is a very high probability that it is encoded in base64.
= is added (at the end of the string) as padding to match a specific number of characters in a string. You can read more about it in this answer.
To answer your question, I will try decoding the given token with and without == at the end, I'll use this online decoder, so you can try it at your end aswell.
With ==:
{"iv":"xooACUwGd59C0jA3JicqMA==","value":"Isnv3b4OqnkMUglCIvH4raCOAr+LbKofLT765kmzvFcCdp4oC1UBVN04e4WS8rZ6","mac":"f4cc6c6beb1aefe4f1b95da50a7bf3ec4a1b540f0aefa188413b4a1e1ece5a09"}
Without ==:
{"iv":"xooACUwGd59C0jA3JicqMA==","value":"Isnv3b4OqnkMUglCIvH4raCOAr+LbKofLT765kmzvFcCdp4oC1UBVN04e4WS8rZ6","mac":"f4cc6c6beb1aefe4f1b95da50a7bf3ec4a1b540f0aefa188413b4a1e1ece5a09"}
They are same.
It works because they (=) are just padding and they DO NOT contain any information.
I am no laravel expert but, I am guessing the reason it doesn't work with %3D is because it is not decoding the url.

php filter a string so that json_encode does not error out

I'm grabbing a bunch of data from a database and putting it into a PHP array. I'm then looking to json_encode that array using $output = json_encode($out).
My issue is that from time to time, something in the array is not able to be read by json_encode and the whole thing fails. If I use print_r($out) to have a look, I can clearly see where it's failing, because the character that is screwing things up always appears as a question mark inside of a black diamond �.
First - what are these characters?
Second - Is there a function I can pass the elements through prior to adding them to the array that would strip these out, or replace 'them' with blanks?
I found the answer to this. Since the data coming FROM the database was stored with the "black diamond" character, I needed to get this out POST grabbing it from the database.
$x[4] = utf8_encode(odbc_result($query, 'B'));
By passing the result through utf8_encode, the string is encoded into UTF-8 and the illegal character is removed.
Say echo json_encode($out);
This will solve your issue
Black diamonds are browser issue. Database uses plain question marks.
It seems you are getting already wrong data from databalse. But that's quite tricky to have incorrect utf with your settings. You need to check everything
if your table marked with utf8 charset
if your data indeed encoded in utf (not marked but indeed encoded)
if your server sending correct charset in Content-type header.
it is also useful to see the page choosing different charsets from your browser menu.
But first of all you have to wipe any trace of all random actions you tried, all these various encode, decode and stuff. Just plain and direct output from database. Otherwise you will never get to the problem

mysqli php query doesnt insert character "+"

I'm using php and mysqli and I meet a problem with an insert query which looks like :
SET NAMES 'utf8'
$text = mysqli_real_escape_string($connection, $text)
insert into table values('', '".$text."');
Pages are encoded utf8 without BOM and mysql is utf8 general ci
The problem is when I use phpmyadmin the request works fine but when I use website interface and type a text with character "+" it replace with a space " " in mysql but all other characters like ', ", accents, \, /, % are correctly inserted...
It worked before I probably made a mistake.
Thanks you by advance and sorry for my poor english.
It is neither mysql, not mysqli, not PHP.
None of them put any special meaning in this character.
If you care to verify your inserts, by simply echoing $text out before insert, you will see that it is already stripped of + sign. So, you have to find the code that strips that symbol out.
A program is not a "black box" which you feed with data and it returns some unexpected output.
But rather set of operators, each performing some data manipulations.
So, you have to debug your code, means you have to echo your $text variable out in various parts of your code to see where it gets changed. Most likely it is getting some unnecessary treatment. After finding that code you may either remove it or ask here if it ok or not.
The only possible case of automated replacement of + character would be if you type your text right in the browser's address bar. In this case + can be replaced with space automatically as PHP does decode urlencoded text and + is used to substitute space character in the URL

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.

Categories