PHP Two common examples of curl function _php Tutorial

Source: Internet
Author: User
example, Analog landing

The code is as follows
/**
* Analog Login
* $url Request Address
* $post data required for post
* Cookies that are taken $cookie landing
* $cookiejar where the cookie is to be stored such as/tmp/test.cookie
* $referer address on page
*
**/
function Vcurl ($url, $post = ", $cookie =", $cookiejar = ", $referer =") {
$tmpInfo = ";
Files used to store cookies
Initialize Curl
$curl = Curl_init ();
Set Destination URL
curl_setopt ($curl, Curlopt_url, $url);
Using the browser proxy that is currently in use
curl_setopt ($curl, curlopt_useragent, $_server[' http_user_agent ');
curl_setopt ($curl, Curlopt_useragent, "mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0) ");
Set the Referer header if there is a ref parameter, or the Referer header is set automatically
if ($referer) {
curl_setopt ($curl, Curlopt_referer, $referer);
} else {
curl_setopt ($curl, Curlopt_autoreferer, 1);
}
If there is a post data parameter, the method is post, and the data is set, otherwise the get
if ($post) {
Send a regular POST request with the default type: Application/x-www-form-urlencoded,www.111cn.net form submission
curl_setopt ($curl, Curlopt_post, 1);
curl_setopt ($curl, Curlopt_postfields, $post);
}
If there is a cookie parameter, set the
if ($cookie) {
curl_setopt ($curl, Curlopt_cookie, $cookie);
}
If there is a cookie file parameter, set the access cookie filename
if ($cookiejar) {
curl_setopt ($curl, Curlopt_cookiejar, $cookiejar);
curl_setopt ($curl, Curlopt_cookiefile, $cookiejar);
}
If 302 is transferred, return the transferred URL and content
curl_setopt ($curl, curlopt_followlocation, 1);
Set the maximum number of seconds to execute
curl_setopt ($curl, curlopt_timeout, 100);
Returns whether the content contains header information
curl_setopt ($curl, Curlopt_header, 0);
The returned result exists in a file or variable, not directly in the browser
curl_setopt ($curl, Curlopt_returntransfer, 1);
Return results after executing a function
$tmpInfo = curl_exec ($curl);
If an error occurs, a message is displayed
if (Curl_errno ($curl)) {
$tmpInfo = '
Error:
'. Curl_error ($curl);
}
Close Curl Object
Curl_close ($curl);
return results
return $tmpInfo;
}

example, Soap

The code is as follows
function Vcurlsoap ($url, $SoapRequest, $SoapAction) {
$ch = Curl_init (); Initiate the Curl session
curl_setopt ($ch, Curlopt_url, $url); Set to URL to post to
curl_setopt ($ch, Curlopt_returntransfer, 1); Return data in a variable
curl_setopt ($ch, Curlopt_header, 0);
curl_setopt ($ch, Curlopt_post, 1);
curl_setopt ($ch, Curlopt_postfields, $SoapRequest); Post the XML
curl_setopt ($ch, curlopt_timeout, 60); Set timeout in seconds
curl_setopt ($ch, Curlopt_ssl_verifypeer, 0);
$header = Array ("Content-type:text/xml");
$header [] = "content-length:". strlen ($SoapRequest);
if (! Is_null ($SoapAction))
$header [] = ' soapaction: '. $SoapAction. '"';
curl_setopt ($ch, Curlopt_httpheader, $header);
$xmlResponse = curl_exec ($ch);
Curl_close ($ch);
return $xmlResponse;
}

http://www.bkjia.com/PHPjc/730240.html www.bkjia.com true http://www.bkjia.com/PHPjc/730240.html techarticle For example, the analog login code is as follows/** * Analog login * $url Request address * $post required POST data * $cookie the cookie to be taken at login * $cookiejar the place where the cookie is to be stored .

  • 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.