-1

I am trying to pass across a string to receive a response. In the URL, at the end for the Password= field, there is a Exclamation Point in the string. This is causing a fail in the code - I have included an example below

$getResponse    = file_get_contents("https://LINK/&Password=pass!word");
echo "<br>Response:";var_dump($getResponse);die;

I have tried both as you see above AND by putting \ in front of the Exclamation Point. Any assistance would be greatly apprecieated!

ArcticMediaRyan
  • 687
  • 8
  • 32
  • https://stackoverflow.com/questions/34620810/how-to-retrieve-the-data-from-a-url-with-an-exclamation-mark-inside-it – Thomas Apr 23 '18 at 05:43
  • That URL looks weird. Are you sure you're not missing something? Like a `?` at some point? That URL would actually look for a resource called `&Password=pass!word`, not sending Password as a parameter. – M. Eriksson Apr 23 '18 at 05:43
  • To keep the question simple I did remove a tonne of data. Yes you are correct it should have a ? at the start but for the purpose of this question it was not needed. – ArcticMediaRyan Apr 23 '18 at 05:45
  • 1
    Then you have your answer below. `urlencode()` is the solution. – M. Eriksson Apr 23 '18 at 05:46
  • Not related to `file_get_contents()` in any way. You fail to build a properly encoded URL. Always use [`urlencode()`](http://php.net/manual/en/function.urlencode.php) to properly encode the dynamic data when you build an URL. – axiac Apr 23 '18 at 06:01

2 Answers2

1

You need to encode non-ASCII characters (!) into a format that can be transmitted over the Internet.

Try this:


    $url=urlencode("https://LINK/&Password=pass!word");
    $getResponse    = file_get_contents($url);
    echo "Response:";var_dump($getResponse);die;

Urlencode Help on PHP

0

Try escaping password in url:

$pswd = urlencode('pass!word'); /* or use - rawurlencode */
$url = 'https://example.com/page?Password='.$pswd; /* replaced `&` with `?` */
$getResponse = file_get_contents($url);