PHP's curl implements Get,post and cookies (instance)

Source: Internet
Author: User
Tags set cookie vars

Similar to dreamhost such as host service providers, is to display the use of fopen. Use of PHP's curl can be implemented to support FTP, FTPS, HTTP htpps SCP SFTP TFTP, TELNET DICT file and LDAP. Curl supports SSL certificates, HTTP POST, http PUT, FTP uploads, Kerberos, HTT-based uploads, proxies, cookies, user + password proofs, File transfer recovery, HTTP proxy channels are most commonly used, HTTP-based Get and Post methods.

Code implementation:

1. Get implementation of HTTP

$ch= Curl_init ("Http://www.domain.com/api/index.php?test=1"); curl_setopt ($ch, Curlopt_returntransfer,true) ;//Get Data backcurl_setopt ($ch, Curlopt_binarytransfer,true) ;//gets the data back when Curlopt_returntransfer is enabledEcho $output= Curl_exec ($ch) ;/*Write File*/$fh=fopen("Out.html", ' W ') ;fwrite($fh,$output) ;fclose($fh) ;

2, the HTTP POST implementation

<?PHP$url= ' http://www.domain.com/api/' ;$fields=Array(               ' lname ' = ' justcoding ', ' fname ' = ' phplover ', ' title ' = ' MyApi ', ' age ' =& gt; ' ' Email ' = ' [email protected] ', ' phone ' = ' 1353777303 '              );//$post _data = Implode (' & ', $fields);//open connection$ch=curl_init ();//set the URL, number of post VARs, post datacurl_setopt ($ch, Curlopt_url,$url); curl_setopt ($ch, Curlopt_post,Count($fields)) ;//when enabled, a regular post request is sent with the type: application/x-www-form-urlencoded, just as the form was submitted. curl_setopt ($ch, Curlopt_postfields,$fields);//The "POST" operation in HTTP. If you want to transfer a file, you need a filename at the start of the @Ob_start(); Curl_exec ($ch);$result=ob_get_contents() ;Ob_end_clean();Echo $result;//Close ConnectionCurl_close ($ch) ;

3. PHP's Curl Transfer cookie

Two different ways:

One is automatic:

curl_setopt ($curlHandle, Curlopt_cookiejar, ' cookie.txt '); //Save

curl_setopt ($curlHandle, Curlopt_cookiefile, ' cookie.txt '); //Read

This will automatically keep the cookie up.
But two times, one is to first access the cookie, then the link to use the cookie

<?PHPfunctionGet_curlcuconent2 ($filename,$referer){   $cookie _jar=Tempnam('./tmp ', ' Jsessionid '); $ch=Curl_init (); curl_setopt ($ch, Curlopt_url,$filename); curl_setopt ($ch, Curlopt_header,false); curl_setopt ($ch, Curlopt_returntransfer, 1); //Set cookie path for file read and submitcurl_setopt ($ch, Curlopt_cookiejar,$cookie _jar); $filecontent=curl_exec ($ch); Curl_close ($ch); $ch=Curl_init (); $hostname= "Www.domain.com"; //$referer = "http://www.domain.com/";curl_setopt ($ch, Curlopt_url,$filename); curl_setopt ($ch, Curlopt_referer,$referer);//look here, you can also say you come from Googlecurl_setopt ($ch, Curlopt_useragent, "www.domain.com"); //$request = "jsessionid=abc6szw15ozvz_pu9b-8r";//Set Post Parameters//curl_setopt ($ch, Curlopt_postfields, $request); Above this sentence, of course you can say you are Baidu, get rid of the value here is OK, can realize the function of thieves, $_server[' http_user_agent '//You can also make a spider yourself, then disguised here Curlopt_ UserAgent Bar//If you want to put this program on Linux with Php-q execution that also to write specific $_server[' http_user_agent ', forged can alsocurl_setopt ($ch, Curlopt_returntransfer, 1); curl_setopt ($ch, Curlopt_cookiefile,$cookie _jar); curl_setopt ($ch, Curlopt_header,false);//set whether to output page contentcurl_setopt ($ch, Curlopt_get, 1);//Post,get Past   $filecontent= Curl_exec ($ch); Preg_match_all("/charset= (. +?) [null\ "\ ']/is",$filecontent,$charsetarray); if(Strtolower($charsetarray[1] [0]) = = "Utf-8")         $filecontent=Iconv(' Utf-8 ', ' Gb18030//ignore ',$filecontent); Curl_close ($ch); return $filecontent;}?>

Another type of customization:

$header[]= ' Accept:image/gif, Image/x-xbitmap, Image/jpeg, Image/pjpeg, Application/x-shockwave-flash, text/html, * '. ‘/* ‘;$header[]= ' ACCEPT-LANGUAGE:ZH-CN ';$header[]= ' user-agent:mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1;. NET CLR 2.0.50727) ';$header[]= ' Host: '.$ your target host;$header[]= ' connection:keep-alive ';$header[]= ' Cookie: '.$ your cookie string; curl_setopt ($curlHandel, Curlopt_httpheader,$header);

Original link http://justcoding.iteye.com/blog/842371

PHP's curl implements Get,post and cookies (instance)

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.