PHP Curl Get,post,cookie

Source: Internet
Author: User
Tags set cookie
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.

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 back
curl_setopt ($ch, Curlopt_binarytransfer, true); Gets the data back when Curlopt_returntransfer is enabled
echo $output = curl_exec ($ch);

/* Write file */
$fh = fopen ("out.html", ' W ');
Fwrite ($FH, $output);
Fclose ($FH);

2, the HTTP POST implementation

$url = ' http://www.domain.com/api/';
$fields = Array (
' lname ' = ' justcoding ',
' fname ' = ' phplover ',
' Title ' = ' MyApi ',
' Age ' = ' 27 ',
' Email ' = ' 1353777303@gmail.com ',
' Phone ' = ' 1353777303 '
);
$post _data = Implode (' & ', $fields);

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)); 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 connection
Curl_close ($ch);

if ($_get[' test ')
{
Print_r ($_get);
}

if ($_post)
{
Print_r ($_post);
}

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

Example:

function Get_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 submit
curl_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 Google
curl_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 OK, can realize the function of Thief, $_server[' http_user_agent ']
You can make yourself a spider, so pretend to be the curlopt_useragent.
If you want to put this program on Linux with Php-q execution that also write specific $_server[' http_user_agent ', forged can also
curl_setopt ($ch, Curlopt_returntransfer, 1);
curl_setopt ($ch, Curlopt_cookiefile, $cookie _jar);
curl_setopt ($ch, Curlopt_header, false);//Set whether to output page content
curl_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;
}

?>

A custom:

$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);

  • Related Article

    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.