Common way for PHP to send post requests

Source: Internet
Author: User

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));

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.