Link to home
Start Free TrialLog in
Avatar of rgb192
rgb192Flag for United States of America

asked on

writing a post to a file that is on same folder and same webserver

I had this question after viewing Really simple no curl. Send a post 3 times.


Related question explained complex object oriented code so i can understand moving parts

I would like a form example but hard coded so no html text boxes

I still dont know what is happening in a post web form
and a webform has been described to me many times and I just memorize and/or copy/Paste code

no security is needed.
no extensiblity ; I dont need a library that i will re use
                   and I will not use this code on a file outside webserver folder

send associate array  to url on my server
another file on same server

maybe just one line
file_put_contents('file.txt "    . myArray()   ."\n", FILE_APPEND);


but then I am just writing an array to a text file not an actual post

I guess I dont even know what a post is
and I just ready php.net post page many times
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
a form example but hard coded so no html text boxes
That's kind of an oxymoron.  Forms are HTML.  The input controls are integral to the operation of the form.  They assemble the information that is sent with the HTTP request when the form is submitted.

PHP file_put_contents() writes a string into a file on your server.  It does not make an HTTP request.

Here is an explanation of the client/server relationship and the HTTP request.  There is a lot more to it than this, but this is enough to know to get started.  Take your time to read it carefully, because you have to understand this to understand the answer to your question.
https://www.experts-exchange.com/articles/11271/Understanding-Client-Server-Protocols-and-Web-Applications.html

A POST is a type of HTTP request.  A GET is another type of HTTP request.  There are other types of requests, but GET and POST are the most common and the only ones you need to think about most of the time.  GET requests are used to "get" information from a web page or an API.  Any time you use your browser to read a web page, your browser is making a GET-method request.  By definition, GET requests must not change any information on the server.  The information you send in a GET request is appended to the URL, and it's called the "query string."

But what if you want to change information on the server?  Like maybe purchase a product or upload a file?  Then you make a POST request, sending along the information you want to change.  It's not sent in the URL; it's sent separately.  In the world of HTML and WWW forms, it's sent in the input controls of the HTML forms.  

A PHP script can pretend to be a web browser (or other client, in the client/server model).

To simplify the many things that have to be done in an HTTP request, we can use a PHP script and the cURL library.  As you look at the script here, just skip over anything above line 75 - it's all code that you don't need to worry about.  Treat it like a black box.

The usage example is a three-part thing.  You have to set the information you want to change into an array.  This has to be an associative array, with key names that are known to the script you're POSTing to.  For this example we can use anything - the point here is just to show you how to use the setup.  This is done on line 76 and 77.  You can add more key/value pairs, as might be needed for your application.

You have to set the URL of the script you're POSTing to.  This is done on line 80.  This is just my test script.  You would put the URL of your application here.  I believe this may need to be a fully-qualified URL path that is routable over the internet- a relative path on your own server might not work.  I have never tested cURL with a relative path.

You have to call the cURL class to get the response from the URL.  This is done on line 83.  You give it the URL and the name of the associative array with your key/value pairs.

If the script at the URL is able to interpret your POST request, it will send back a response document, and the code on line 87 will show the document.  If the script fails, the response document will contain boolean FALSE, and the cURL object will contain the error information.

That's it!  That is all there is to it.

<?php // demo/curl_post_example.php
/**
 * Demonstrate how to use cURL to make a POST request
 * This may be used to start an asynchronous process
 * Property 'title' is a comment field to identify the object
 *
 * http://php.net/manual/en/book.curl.php
 * http://curl.haxx.se/libcurl/c/libcurl-errors.html
 */
error_reporting(E_ALL);

Class POST_Response_Object
{
    public $href, $title, $http_code, $errno, $info, $document;

    public function __construct($href, $post_array=[], $title=NULL)
    {
        // ACTIVATE THIS TO AVOID TIMEOUT FOR LONG RUNNING SCRIPT
        // set_time_limit(10);

        // STORE THE CALL INFORMATION
        $this->href  = $href;
        $this->title = $title;

        // CREATE THE REFERRER
        $refer = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

        // PREPARE THE POST STRING
        $post_string = http_build_query($post_array);

        // MAKE THE REQUEST
        if (!$this->my_curl($href, $post_string, $refer))
        {
            // ACTIVATE THIS TO SEE THE ERRORS AS THEY OCCUR
            // trigger_error("Errno: $this->errno; HTTP: $this->http_code; URL: $this->href", E_USER_WARNING);
        }
    }

    protected function my_curl($url, $post_string, $refer, $timeout=3)
    {
        // PREPARE THE CURL CALL
        $curl = curl_init();
        curl_setopt( $curl, CURLOPT_URL,            $url           );
        curl_setopt( $curl, CURLOPT_REFERER,        $refer         );
        curl_setopt( $curl, CURLOPT_HEADER,         FALSE          );
        curl_setopt( $curl, CURLOPT_POST,           TRUE           );
        curl_setopt( $curl, CURLOPT_POSTFIELDS,     $post_string   );
        curl_setopt( $curl, CURLOPT_ENCODING,       'gzip,deflate' );
        curl_setopt( $curl, CURLOPT_TIMEOUT,        $timeout       );
        curl_setopt( $curl, CURLOPT_RETURNTRANSFER, TRUE           );
        curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, TRUE           );
        curl_setopt( $curl, CURLOPT_FAILONERROR,    TRUE           );

        // IF USING SSL, THIS INFORMATION MAY BE IMPORTANT
        // http://php.net/manual/en/function.curl-setopt.php#110457
        // http://php.net/manual/en/function.curl-setopt.php#115993
        // http://php.net/manual/en/function.curl-setopt.php#113754
        // REDACTED IN 2015 curl_setopt( $curl, CURLOPT_SSLVERSION, 3 );
        curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, FALSE  );
        curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, FALSE  );

        // RUN THE CURL REQUEST AND GET THE RESULTS
        $this->document  = curl_exec($curl);
        $this->errno     = curl_errno($curl);
        $this->info      = curl_getinfo($curl);
        $this->http_code = $this->info['http_code'];
        curl_close($curl);

        // RETURN DOCUMENT SUCCESS SIGNAL
        return $this->document;
    }
}


// USAGE EXAMPLE CREATES ASSOCIATIVE ARRAY OF KEY=>VALUE PAIRS
$args["name"] = 'Ray';
$args["mail"] = 'Ray.Paseur@Gmail.com';

// SET THE URL
$url = "https://Iconoun.com/demo/request_reflector.php";

// CREATE THE RESPONSE OBJECT
$resp = new POST_Response_Object($url, $args);

// SHOW THE RESPONSE OBJECT
echo '<pre>';
var_dump($resp);

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rgb192

ASKER

easiest solution is.  <form action="targeturl.php" method="post">
  <input type="hidden" name="postvar1" value="1" />
  <input type="hidden" name="postvar2" value="cat" />
  <input type="hidden" name="postvar3" value="2017-01-01" />
  <input type="hidden" name="postvar4" value="jack@frost.com" />
  <input type="submit" />
Avatar of rgb192

ASKER

Julian has best solution 250 points and Ray has assisted solution 250 points. Thanks for teaching me about POST simplifying problem so I can understand better.