Source-php-request-2-php tutorial

Source: Internet
Author: User
Source-php-request-2 php is more pitfall place is to achieve the same purpose, can use a variety of super means. For example, (file_get_contents, fopen, curl, fsockopen, and socket) is really difficult for a programmer with less experience. ,

So in many cases, we need to compare. Search for irresponsible resources on the Internet and make a prediction. Then compare and weigh the values.

The following is an excerpt:

Function curlRequest ($ url, $ isPost = false, $ param = array (), $ cookie = null, $ header = null, $ referer = "", $ timeout = 60) {$ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ url); if (is_array ($ header )&&! Empty ($ header) {// Set header $ set_head = array (); foreach ($ header as $ k = >$ v) {$ set_head [] = "$ k: $ v ";} curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ set_head);} else {curl_setopt ($ ch, CURLOPT_HEADER, 0);} if ($ isPost = true) {curl_setopt ($ ch, CURLOPT_POST, $ isPost); curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ param);} if (! Empty ($ cookie) {if (is_array ($ cookie) {$ cookies = implode (';', $ cookie) ;} else {$ cookies = $ cookie ;} curl_setopt ($ ch, CURLOPT_COOKIE, $ cookies);} if (! Empty ($ referer) {curl_setopt ($ ch, CURLOPT_REFERER, $ referer);} if ($ timeout> 0) {curl_setopt ($ ch, CURLOPT_TIMEOUT, $ timeout );} curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true); $ ret = curl_exec ($ ch); if (curl_errno ($ ch) {$ ret = '';} curl_close ($ ch); return $ ret ;}

The general process will not be so complicated:

 

Note that curl cannot run on the console. for example, php xxx. php params_1 requires a server.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.