0

I have to write a php file that was supposed to receive data from a site form with post method . To test it i wrote these two files :

<?php

    $url = 'temp.php';
    $data = array('username' => 'jsafsd@gmail.com', 'password' => 'lassrd');
    $query=http_build_query($data);
    $options = array(
        'header' => "Connection: close\r\n".
                            "Content-Length: ".strlen($query)."\r\n",
        'method'  => 'POST',
        'content' => $query,
    );


    $context  = stream_context_create(array ( 'http' => $options ));
    $result = file_get_contents($url,false,$context);
    var_dump($result);

?>

temp.php

<?php
    if($_POST['username'] && $_POST['password'])
    echo "hi";
    else 
    echo "bye";
    exit();
?>

But when i run the first file, all i get is string

<?php
    if($_POST['username'] && $_POST['password'])
    echo "hi";
    else 
    echo "bye";
    exit();
?>

(length=95)

What is the problem with this ?

Gopal Joshi
  • 2,350
  • 22
  • 49
J V
  • 515
  • 1
  • 5
  • 15
  • Why do you not use cURL to do that. That is very good php extension to handle all GET, POST, DELETE and PUT methods – Manish Jangir Jan 02 '14 at 04:54
  • 4
    filte_get_contents will give you the content of the file, that's exactly what it returned. In you case, you might want to wrap the file name with http schema. $url ='http;//yourhost/temp.php' – David Lin Jan 02 '14 at 04:55
  • try using parse_str($_POST,$output) in temp.php file. The $output will contain the posted data – Mahavir Munot Jan 02 '14 at 05:00
  • use eval() php function to execute string as a php code like this: eval($result); http://stackoverflow.com/questions/10866301/execute-php-code-in-a-string – Awlad Liton Jan 02 '14 at 05:02

3 Answers3

0

Php manual has an example of it. http://php.net/stream_context_create

In your case it will be:

$context  = stream_context_create(array ( 'http' => $options ));
$fp = fopen($url, 'r', false, $context); // url to your temp.php file
fpassthru($fp);
fclose($fp);

Also to get rid of notice you need to add Content-Type header.

$options = array(
    'header' => "Connection: close\r\n".
        "Content-Length: ".strlen($query)."\r\n".
        "Content-Type: application/x-www-form-urlencoded\r\n", // add this line
    'method'  => 'POST',
    'content' => $query,
);

Curl is the standard tool for generating this kind of requests.

chanchal118
  • 3,551
  • 2
  • 26
  • 52
0

Use curl :

File 1 1.php

    <?php

    $url = 'http://localhost/your_project_path/2.php';

    $data = array('username' => 'jsafsd@gmail.com', 'password' => 'lassrd');

    $ch = curl_init($url);

    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,CURLOPT_AUTOREFERER,1);
    curl_setopt($ch,CURLOPT_POST,1);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$data);

    $result = curl_exec($ch);


    curl_close($ch);

    print_r($result);

File 2 (2.php):

    if(isset($_POST['username']) && $_POST['username'] && isset($_POST['password']) && $_POST['password'])
        echo "hi";
    else
        echo "bye";
0

You'll need a compiler such as WampServer to render the PHP file.; web browsers usually only render it as a string.