PHP Curl Implementation Get,post and Cookie (example introduction) _php Tips

Source: Internet
Author: User
Tags auth curl http post

Similar to the DreamHost such host service providers, is to show the use of fopen. Using PHP's curl can be implemented to support FTP, FTPS, HTTP htpps SCP SFTP TFTP TELNET DICT file and LDAP. Curl support SSL certificates, HTTP POST, HTTP put, FTP upload, Kerberos, htt format based upload, proxy, cookie, user + password proof, File transfer recovery, HTTP Proxy channel is the most commonly used, is based on HTTP Get and Post methods.
Code implementation:
1. HTTP GET implementation

Copy Code code as follows:

$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. Post implementation of HTTP
Copy Code code as follows:

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

Note:The parameters of the POST request are concatenated as a string by the Get method:
such as:$params = ' userid= '. $this->user_id. ' &auth= '. $this->auth. ' &sig= '. $this->sig

There are also cross-platform requests, curl_setopt ($ch, curlopt_followlocation, 1); Use automatic jump (very important)

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 like the form submission.
curl_setopt ($ch, Curlopt_postfields, $fields); The POST operation in HTTP. If you want to transfer a file, you need a filename at the beginning of @

Ob_start ();
Curl_exec ($ch);
$result = Ob_get_contents ();
Ob_end_clean ();

echo $result;

Close connection
Curl_close ($ch);


Copy Code code as follows:

<?php

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

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

PHP's Curl transfer cookie

Two kinds of ways:
One is automatic:
Copy Code code as follows:

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

So the cookie will follow up automatically.
However, two times, first access to generate cookies, then link to use cookies
Example:
Copy Code code as follows:

<?php

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 the cookie path that the file reads and submits
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're 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 the Thief, $_server[' http_user_agent '
You can make a spider yourself, then disguise the curlopt_useragent here.
If you're going to put this program on Linux and execute it with PHP-Q, write the exact $_server[' http_user_agent ', or fake it.
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:
Copy Code code as follows:

$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 []= ' Cookies: '. $ 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.