php的curl实现get和post的代码

yipeiwu_com5年前PHP代码库
curl 支持SSL证书、HTTP POST、HTTP PUT 、FTP 上传,kerberos、基于HTT格式的上传、代理、cookie、用户+口令证明、文件传送恢复、http代理通道就最常用的来说,是基于http的get和post方法。

代码实现:

1、http的get实现
复制代码 代码如下:

$ch = curl_init("//www.jb51.net/") ;
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ;
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ;
$output = curl_exec($ch) ;
$fh = fopen("out.html", 'w') ;
fwrite($fh, $output) ;
fclose($fh) ;

2、http的post实现
复制代码 代码如下:

//extract data from the post
extract($_POST) ;
//set POST variables
$url = '//www.jb51.net/get-post.php' ;
$fields = array(
'lname'=>urlencode($last_name) ,
'fname'=>urlencode($first_name) ,
'title'=>urlencode($title) ,
'company'=>urlencode($institution) ,
'age'=>urlencode($age) ,
'email'=>urlencode($email) ,
'phone'=>urlencode($phone)
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&' ; }
rtrim($fields_string ,'&') ;
//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)) ;
curl_setopt($ch, CURLOPT_POSTFIELDS,$fields_string) ;
//execute post
$result = curl_exec($ch) ;
//close connection
curl_close($ch) ;

相关文章

PHP随机数生成代码与使用实例分析

PHP随机数生成代码与使用实例分析

我们还可以使用随机数设计任何我们想象的程序结构。 首先来认识一下PHP提供的随机数函数rand()。PHP的rand()函数将返回随机整数,具体使用方法如下 rand(min,max)...

在PHP 7下安装Swoole与Yar,Yaf的方法教程

在PHP 7下安装Swoole与Yar,Yaf的方法教程

本文开发坏境: CentOS 7 PHP 7.0.16 安装PECL //php版本 > 7 $ wget http://pear.php.net/go-pear....

PHP COOKIE设置为浏览器进程

例如: 复制代码 代码如下:<?php $USERID="PHPer"; $CookieTime=0; setcookie("USERID", "$USERID", time()+...

php中多维数组按指定value排序的实现代码

呵呵,业务需要按多维数组中某个元素进行排序,在PHP中也是非常容易实现的,一个函数调用一个回调函数就搞定了。贴出代码: 复制代码 代码如下: $arr = array( &nb...

php脚本守护进程原理与实现方法详解

本文实例讲述了php脚本守护进程原理与实现方法。分享给大家供大家参考,具体如下: 思路: 1. while 循环,若当前没有数据要操作可以休眠; 2. crontab 脚本每隔固定时间段...