Ways to implement get and post requests using curl in PHP

Source: Internet
Author: User

1.CURL Introduction
Curl is a tool that uses URL syntax rules to transfer files and data. Supports many protocols, such as HTTP, FTP, Telnet, and so on.
Fortunately, PHP also supports the Curl library. This article describes some of the advanced features of curl and how to use it in PHP.
2. Basic structure
Before learning more complex features, take a look at the basic steps to build a curl request in PHP:
(1) Initialize Curl_init ()
(2) Set Variable curl_setopt ()//The most important thing is here. There is a long list of curl parameters that can be set to specify the details of the URL request. It may be difficult to read them all at once and to understand them, so today we'll just try some of the more commonly used and more useful options.
(3) Execute and get results curl_exec ()
(4) Release curl handle Curl_close ()
3.CURL implementation of Get and post
1), Get mode implementation
Initialization
$ch = Curl_init ();
Setting options, including URLs
curl_setopt ($ch, Curlopt_url, "http://www.xxx.com");
curl_setopt ($ch, Curlopt_returntransfer, 1);
curl_setopt ($ch, Curlopt_header, 0);
Execute and get HTML document content
$output = curl_exec ($ch);
Releasing the curl handle
Curl_close ($ch);
Print the obtained data
Print_r ($output);
2), Post mode implementation
$url = "http://localhost/web_services.php";
$post _data = Array ("username" = "uname", "key" = "123456");
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_url, $url);
curl_setopt ($ch, Curlopt_returntransfer, 1);
Post data
curl_setopt ($ch, Curlopt_post, 1);
Variables for post
curl_setopt ($ch, Curlopt_postfields, $post _data);
$output = curl_exec ($ch);
Curl_close ($ch);
Print the obtained data
Print_r ($output);
The data obtained in this way is in JSON format, using the Json_decode function to interpret the array.
$output _array = Json_decode ($output, true);
If you use Json_decode ($output) parsing, you will get data of type object.


$url = ' Http://commoncgi.dev.wepiao.com/common/default/decrypt ';

$post _data = Array ("Channelid" = "501", "str" = = $token);

Private Function Genurl ($url, $post _data) {

$ch = Curl_init ();
curl_setopt ($ch, Curlopt_url, $url);
curl_setopt ($ch, Curlopt_post, 1);
curl_setopt ($ch, Curlopt_header, 0);
curl_setopt ($ch, Curlopt_returntransfer, 1);
curl_setopt ($ch, curlopt_timeout,10);
curl_setopt ($ch, Curlopt_postfields, $post _data);
$return = curl_exec ($ch);
Curl_close ($ch);
return $return;

}







This article is from the "Chuncoquan" blog, make sure to keep this source http://kequan.blog.51cto.com/10815369/1767452

Ways to implement get and post requests using curl in PHP

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.