Common methods for PHP to send POST requests: PHP Time: June 6, 2016
During PHP Development, POST requests are often sent. POST requests are much safer than GET requests, and the amount of data transmitted is also large. The following PHP programmer Lei Xuesong will take you together to summarize several common methods for PHP to send POST requests, using curl and file_get_content to implement POST requests and transmit parameters respectively.
1. curl implements php post requests and transmits parameters.
$ Data = array ("username" => "raykaeso", "name" => "rayxuesong"); // post parameter $ url = "http://www.leixuesong.cn "; $ ch = curl_init (); // create a connection curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, CURLOPT_POST, 1); curl_setopt ($ ch, CURLOPT_POSTFIELDS, http_build_query ($ data); // Convert the array to a URL request string. Otherwise, the server may not receive the parameter curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 ); // receive html code from the server rather than curl_setopt ($ ch, CURLOPT_HEADER, false) from the browser; $ responds = curl_exec ($ ch ); // accept the response curl_close ($ ch); // close the connection2. file_get_content implements php post request and transmission parameters
$ Data = array ("username" => "raykaeso", "name" => "rayxuesong"); // post parameter $ url = "http://www.leixuesong.cn "; $ content = http_build_query ($ data); $ length = strlen ($ content); $ options = array ('http' => array ('method' => 'post ', 'header' => "Content-type: application/x-www-form-urlencoded \ r \ n ". "Content-length: $ length \ r \ n", 'content' => $ content); file_get_contents ($ url, false, stream_context_create ($ options ));