In the process of PHP development, often need to send post requests, Post compared to get a lot more secure, and the amount of data transferred is also large. The following PHP programmer Rechesson to bring you together to summarize the several common ways of PHP to send post requests, respectively, using curl, file_get_content to implement post requests and pass parameters.
1, curl Implementation of PHP POST request and pass parameters.
$data =array ("username" => "Raykaeso", "name" => "Rechesson");//post parameter
$url = "Http://www.111cn.net";
$ch = Curl_init ();//Create Connection
curl_setopt ($ch, Curlopt_url, $url);
curl_setopt ($ch, Curlopt_post, 1);
curl_setopt ($ch, Curlopt_postfields, Http_build_query ($data));//convert array to URL request string, or sometimes the server may not receive parameters
curl_setopt ($ch, Curlopt_returntransfer, 1); Receive service-side-scoped HTML code rather than direct browser output
curl_setopt ($ch, Curlopt_header, false);
$responds = curl_exec ($ch);//Accept Response
Curl_close ($ch);//close connection
2, file_get_content implementation of PHP POST request and pass parameters
$data =array ("username" => "Raykaeso", "name" => "Rechesson");//post parameter
$url = "Http://www.111cn.net";
$ Content = Http_build_query ($data);
$length = strlen ($content);
$options = Array (
' http ' => Array (
' method ' => ' POST ',
' header ' =>
"Content-type:applicatio N/x-www-form-urlencoded\r\n ".
"Content-length: $length \ r \ n",
' Content ' => $content
)
);
File_get_contents ($url, False, STREAM_ Context_create ($options));