PHP curl implements get and post

Source: Internet
Author: User
PHP's curl implements get and post. read PHP's curl implements get and post. similar to host service providers such as dreamhost, it shows the use of fopen. Php curl supports FTP, FTPS, HTTPHTPPSSCPSFTPTFTPTELNETDICTFILE, and LDAP. Curl supports SSL certificates and HT

Similar to host service providers such as dreamhost, it shows the use of fopen. Php curl supports FTP, FTPS, http htpps scp sftp tftp telnet dict file and LDAP. Curl supports SSL certificates, http post, http put, and FTP uploads, kerberos, HTT-based Upload, proxy, cookie, user + password proof, file transfer recovery, and http proxy channel are the most common methods based on http get and post.

Code implementation:

1. http get implementation

$ Ch = curl_init ("http://www.devdao.com /");
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true );
Curl_setopt ($ ch, CURLOPT_BINARYTRANSFER, true );
$ Output = curl_exec ($ ch );
$ Fh = fopen ("out.html", 'w ');
Fwrite ($ fh, $ output );
Fclose ($ fh );

2. http post implementation

// Extract data from the post
Extract ($ _ POST );
// Set POST variables
$ Url = 'http: // www.webjx.com/get-post.php ';
$ Fields = array (
'Lname' => urlencode ($ last_name ),
'Fname' => urlencode ($ first_name ),
'Title' => urlencode ($ title ),
'Company' => urlencode ($ institution ),
'Age' => urlencode ($ age ),
'Email '=> urlencode ($ email ),
'Phone' => urlencode ($ phone)
);
// Url-ify the data for the POST
Foreach ($ fields as $ key => $ value) {$ fields_string. = $ key. '='. $ value .'&';}
Rtrim ($ fields_string ,'&');
// Open connection
$ Ch = curl_init ();
// Set the url, number of POST vars, POST data
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_POST, count ($ fields ));
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ fields_string );
// Execute post
$ Result = curl_exec ($ ch );
// Close connection
Curl_close ($ ch );

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.