Common methods for PHP to send POST requests

Source: Internet
Author: User
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 connection
2. 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 ));

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.