rgb192
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.tx t " . 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
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.tx
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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" />
<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" />
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.
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.
Open in new window