How get post url in php?
function pc_post_request($host,$url,$content='') { $timeout = 2; $a = array(); if (is_array($content)) { foreach ($content as $k => $v) { array_push($a,urlencode($k).'='.urlencode($v)); } } $content_string = join('&',$a); $content_length = strlen($content_string); $request_body = "POST $url HTTP/1.0 Host: $host Content-type: application/x-www-form-urlencoded Content-length: $content_length $content_string"; $sh = fsockopen($host,80,&$errno,&$errstr,$timeout) or die("can't open socket to $host: $errno $errstr"); fputs($sh,$request_body); $response = ''; while (! feof($sh)) { $response .= fread($sh,16384); } fclose($sh) or die("Can't close socket handle: $php_errormsg"); list($response_headers,$response_body) = explode("\r\n\r\n",$response,2); $response_header_lines = explode("\r\n",$response_headers); // first line of headers is the HTTP response code $http_response_line = array_shift($response_header_lines); if (preg_match('@^HTTP/[0-9]\.[0-9] ([0-9]{3})@',$http_response_line, $matches)) { $response_code = $matches[1]; } // put the rest of the headers in an array $response_header_array = array(); foreach ($response_header_lines as $header_line) { list($header,$value) = explode(': ',$header_line,2); $response_header_array[$header] = $value; } return array($response_code,$response_header_array,$response_body); } Show In this article, we will know what HTTP GET and POST methods are in PHP, how to implement these HTTP methods & their usage, by understanding them through the examples. HTTP: The Hypertext Transfer Protocol (HTTP) is designed to enable communications between clients and servers. HTTP works as a request-response protocol between a client and server. A web browser may be the client, and an application on a computer that hosts a website may be the server. A client (browser) submits an HTTP request to the server; then the server returns a response to the client. The response contains status information about the request and may also contain the requested content. There are 2 HTTP request methods:
We will understand both these methods in detail through the examples. GET Method: In the GET method, the data is sent as URL parameters that are usually strings of name and value pairs separated by ampersands (&). In general, a URL with GET data will look like this: Example: Consider the below example: http://www.example.com/action.php?name=Sam&weight=55 Here, the bold parts in the URL denote the GET parameters and the italic parts denote the value of those parameters. More than one parameter=value can be embedded in the URL by concatenating with ampersands (&). One can only send simple text data via GET method. Example: This example illustrates the HTTP GET method in PHP. HTML
Output: GET() method Advantages:
Disadvantages:
POST Method: In the POST method, the data is sent to the server as a package in a separate communication with the processing script. Data sent through the POST method will not be visible in the URL. Example: Consider the below example: POST /test/demo_form.php HTTP/1.1 Host: gfs.com SAM=451&MAT=62 The query string (name/weight) is sent in the HTTP message body of a POST request. Example: This example illustrates the HTTP POST method in PHP. Here, we have used the preg_match() function to search string for a pattern, returns true if a pattern exists, otherwise returns false. HTML
Output: POST() method Advantages:
Disadvantages:
Please refer to the Difference between HTTP GET and POST Methods article for the differences between them in detail. How POST URL in PHP?If you're looking to post data to a URL from PHP code itself (without using an html form) it can be done with curl. It will look like this: $url = 'http://www.someurl.com'; $myvars = 'myvar1=' .
What is GET POST in PHP?Get and Post methods are the HTTP request methods used inside the . HTTP protocol enables the communication between the client and the server where a browser can be the client, and an application running on a computer system that hosts your website can be the server.
How do you POST data into a URL?To send data using the HTTP POST method, you must include the data in the body of the HTTP POST message and specify the MIME type of the data with a Content-Type header. Below is an example of an HTTP POST request to send JSON data to the server. The size and data type for HTTP POST requests is not limited.
How pass information in PHP with GET and POST method?The POST method transfers information via HTTP headers. The information is encoded as described in case of GET method and put into a header called QUERY_STRING. The POST method does not have any restriction on data size to be sent. The POST method can be used to send ASCII as well as binary data.
|