Home > Backend Development > PHP Tutorial > PHP curl implementation of get, post and cookie submission examples_PHP tutorial

PHP curl implementation of get, post and cookie submission examples_PHP tutorial

WBOY
Release: 2016-07-13 17:42:41
Original
1023 people have browsed it

Hosting service providers such as dreamhost show the use of fopen. Using PHP's curl can support FTP, FTPS, HTTP HTPPS SCP SFTP TFTP TELNET DICT FILE and LDAP. curl supports SSL certificate, HTTP POST, HTTP PUT, FTP upload, Kerberos, HTTP-based upload, proxy, cookie, user + password certificate, file transfer recovery, http proxy channel. The most commonly used one is http-based get and post method.

1. HTTP get implementation
php code
$ch = curl_init("http://www.domain.com/api /index.php?test=1") ;
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ; // Get data and return
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ; // Will get when CURLOPT_RETURNTRANSFER is enabled Data return
echo $output = curl_exec($ch) ;

/* Write file*/
$fh = fopen("out.html", w) ;
fwrite( $fh, $output) ;
fclose($fh) ;

$ch = curl_init("http://www.domain.com/api/index.php?test=1") ;
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Get data and return
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); // Get data and return
echo when CURLOPT_RETURNTRANSFER is enabled. $output = curl_exec( $ch) ;

/* Write to file*/
$fh = fopen("out.html", w) ;
fwrite($fh, $output) ;
fclose ($fh) ;
?>

$url = http://www.domain.com/api/ ;
$fields = array(
lname=>justcoding ,
>              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)) ; // A regular POST will be sent when enabled Request, type: application/x-www-form-urlencoded, just like form submission.
curl_setopt($ch, CURLOPT_POSTFIELDS,$fields); // "POST" operation in HTTP. If you want to transfer a file, you need a file name starting with @

ob_start();
curl_exec($ch);
$result = ob_get_contents();
ob_end_clean();

echo $result;
//close connection
curl_close($ch) ;
?>

Php code
if ($_GET[test])
{
print_r($_GET);
}

if($_POST)
{
print_r($_POST);
} 
?>
if($_GET[test])
{
print_r($_GET);
}
if($_POST )
{
print_r($_POST);
}
?>


2. http post implementation

Php code $url = http://www.domain.com/api/ ;
$fields = array(
lname=>justcoding ,
fname=>phplover , 🎜>                                                                                                                                                                                      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 will be sent, type: application/x-www-form-urlencoded, like Same as form submission.
curl_setopt($ch, CURLOPT_POSTFIELDS,$fields); // "POST" operation in HTTP. If you want to transfer a file, you need a file name starting with @
ob_start();
curl_exec($ch);
$result = ob_get_contents();
ob_end_clean();
echo $result;
//close connection
curl_close($ch) ;
?>

3. PHP’s curl transmits cookies in two ways
:
One is automatic:
Php code
curl_setopt($curlHandle, CURLOPT_COOKIEJAR, cookie.txt); //Save
curl_setopt($curlHandle, CURLOPT_COOKIEFILE, cookie .txt ); //Read
curl_setopt($curlHandle, CURLOPT_COOKIEJAR, cookie.txt ); //Save
curl_setopt($curlHandle, CURLOPT_COOKIEFILE, cookie.txt ); //Read
?> ;
In this way, COOKIE will automatically follow.
But it needs to be done twice. First, the cookie is generated by the first visit, and then the cookie can be used by the link

Example: Php code
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);
//Setting file Read and submit cookie path
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);
$filecontent=curl_exec($ch);
curl_close($ch);
$hostname ="www.domain.com"; ch, CURLOPT_REFERER, $referer); // Look here, you can also say you are from google
curl_setopt($ch, CURLOPT_USERAGENT, "www.domain.com");

//$request = "JSESSIONID=abc6szw15ozvZ_PU9b-8r"; //Set POST parameters
//curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
// As for the above sentence, of course you can say you are baidu, change it here The value is ok, you can realize the function of the thief, $_SERVER[HTTP_USER_AGENT]
//You can also make a spider yourself, then just disguise the CURLOPT_USERAGENT here
//If you want to put this program If you use php -q to execute on Linux, you must also write out the specific $_SERVER[HTTP_USER_AGENT]. You can also fake it.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 



http://www.bkjia.com/PHPjc/486046.html

www.bkjia.com

true

TechArticleHost service providers such as dreamhost show the use of fopen. Using PHP's curl can support FTP, FTPS, HTTP HTPPS SCP SFTP TFTP TELNET DICT FILE and LDAP. curl supports S...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template