file_get_contents with query string

file_get_contents not working
php file_get_contents post
php file_get_contents https
file_get_contents failed to open stream
file_get_contents php input
wordpress file_get_contents
file_get_contents headers
php read file

i am trying to send email from php i have one php file with all values & other php template file.

(both files are on same server)

i am using file_get_contents to get contents of php template file for example

   $url="emil_form.php";
   $a="uname";
  if(($Content = file_get_contents($url. "?uname=".$a)) === false) {
   $Content = "";
    }

   ...... EMAIL Sending Code ..........

and here is code for emil_form.php (email template file)

    Your Name is : <?php $_GET['uname']; ?>

so once i got data in $Content i can send it by email. but i am getting error unable to open file....

what i want is pass data from original php file to template php file and what will be output of template stored in variable so i can send it by email.

how can do this ?

Thanks

The real problem would be that you try to read a local file with a http query string behind it. The file system don't understand this and looks for a file called "emil_form.php?uname=uname". $_GET / $_POST / etc only works over a http connection.

Try to put a placeholder like "%%NAME%%" in your template and replace this after reading the template.

<?php
$url = "emil_form.php";
$a   = "uname";
if(($Content = file_get_contents($url)) === false) {
   $Content = "";
}
$Content = str_replace('%%NAME%%', $a, $Content); 
// sending mail....

Template will look like this:

Your Name is: %%NAME%%

On failure, file_get_contents() will return FALSE . file_get_contents() is the preferred way to read the contents of a file into a string. It will use memory mapping  I found today the PHP file_get_contents function does not work with URLs containing a query string with spaces in it. The following code example should say it all: <p>Shows how spaces in url’s query string do not work with file_get_contents, but replacing spaces with + does work</p>

Here's an alternative solution... Use the relative path or absolute path as suggested by "cballou" to read file.

But insted of wirting a php file use a simple text file put you message template in it replace <?php $_GET['uname']; ?> with something unique like %uname% (replacement keys). Read the file content into a variable and replace the replacement keys with your variable like so,

   $url = "/usr/local/path/to/email_form.txt";
       $a = "uname";
       if(($Content = file_get_contents($url)) === false) {
           $Content = "";
       }else{
           $content = str_replace('%uname%', $a, $content);
       }

How to use file_get_contents to get a URL with query string attached. $url = “http://sportsweatherstats.com/test/showurl.php?u=this and that&h=that and this”; $html = file_get_contents($url); file_get_contents() is the preferred way to read the contents of a file into a string. It will use memory mapping techniques if supported by your OS to enhance performance. It will use memory mapping techniques if supported by your OS to enhance performance.

$url = sprintf("%s://%s",  isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http', $_SERVER['SERVER_NAME']);
$content = file_get_contents($url."/file.php?test=".$test);
echo $content;

The first method involves using the function file_get_contents. If you need to attach a query string / GET parameters, then you can simply add them onto the  PHP file_get_contents() function is used for reading the file contents into a string. This function is also capable to read content from url. 1. filename is the name of file or url. 2. include_path if enabled, Search for the file in the include_path as well 3.

Please note that you had a filename error in $url which wss trying to load emil_form.php.

A secondary issue seems to be the path you are using for your call to $url. Try making it an full URL in order to properly parse the file, avoiding any templating you may otherwise have to do. The RTT would be slower but you wouldnt have to change any code. This would look like:

   $url = 'http://www.mysite.com/email_form.php';
   $a = "uname";
   if(($Content = file_get_contents($url. "?uname=".$a)) === false) {
       $Content = "";
   }

The file_get_contents() reads a file into a string. This function is the preferred way to read the contents of a file into a string. It will use memory mapping  The file_get_contents() reads a file into a string. This function is the preferred way to read the contents of a file into a string. It will use memory mapping techniques, if this is supported by the server, to enhance performance.

Another useful function can build the query string: http_build_query(). function builds the context that you pass to file_get_contents() . The full  I added a urlencode() to ensure your string is properly escaped, and your first example in the previous post works fine. This may solve your problems.

Request Parameters; Query String Parameters; Request Body Data; PUT, PATCH or use ($path) { return file_get_contents($path); });  I'm not sure how you get this error, since file_get_contents() should receive at least a string with the path. Even though, drupal_parse_url() is able to handle external script files, it's true that GET parameters might change the content of a script file (as you pointed with gmaps and its libraries); so the parsing is not being done correctly.

function _locale_parse_js_file($filepath) { global $language; // The file path might contain a query string, so make sure we only use the // actual  I'm auditing some PHP code for a friend, and there are a couple of file_get_contents usages for GETting URLs where the user-supplied parameters are concatenated without any escaping, e.g.: function

Comments
  • Do you get an error if you directly call emil_form.php?uname=uname...?
  • Excellent first answer, I was just writing the same thing but you beat me to it.
  • What's the difference between "email_form.php" and "./email_form.php"?