Two examples of curl functions commonly used in PHP

Source: Internet
Author: User
Tags curl header

  example, Analog landing

  code as follows  
* Simulate login
* $url request Address
* $post need post data
* $cookie The co that was taken at the time of landing OKIE
* $cookiejar cookie to be saved in the location such as/tmp/test.cookie
* $referer page address
Function Vcurl ($url, $post = ' ', $cookie = ', $cookiejar = ', $referer = ') {
$tmpInfo = ';
///file to hold cookies
//Initialize curl
$curl = curl_in It ();
//Set destination URL
curl_setopt ($curl, Curlopt_url, $url);
//using the browser proxy 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) ");
//If there is a ref parameter, set the Referer header, otherwise the Referer header
if ($referer) {
curl_setopt ($curl, Curlopt_referer, $referer) is set automatically;
} 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, the default typeAs: application/x-www-form-urlencoded, form Submit
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 file name
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 maximum number of seconds to perform
curl_setopt ($curl, Curlopt_timeout, 100);
//Returns whether the header information is included in the content
curl_setopt ($curl, Curlopt_header, 0);
///The returned result is in a file or variable instead of appearing directly in the browser
curl_setopt ($ Curl, Curlopt_returntransfer, 1);
//Return results after function
$tmpInfo = curl_exec ($curl);
//If an error occurs, display an error message
if (Curl_errno ($curl)) {
$tmpInfo = ' <pre ><b> error: </b><br/> '. Curl_error ($curl);
///Close Curl Object
Curl_close ($curl);
//return result

Example, soap

  code is as follows &nbs P;
function vcurlsoap ($url, $SoapRequest, $SoapAction) {
$ch = curl_i NIT (); Initiate the Curl session
curl_setopt ($ch, Curlopt_url, $url)//set to URLs to post to
curl_setopt ($ch, CUR Lopt_returntransfer, 1); Return data in a variable
curl_setopt ($ch, Curlopt_header, 0);
curl_setopt ($ch, Curlopt_post, 1);
Cur L_setopt ($ch, Curlopt_postfields, $SoapRequest); Post the XML
curl_setopt ($ch, curlopt_timeout);//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;
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.