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选择排序法实现数组排序的方法。分享给大家供大家参考。具体分析如下: 选择排序法的基本思路:直接用案例来说明吧,比如有一个数组$arr = array(2,6,3,9)...

php根据生日计算年龄的方法

本文实例讲述了php根据生日计算年龄的方法。分享给大家供大家参考。具体如下: <?php function birthday($birthday){ $age =...

php正则提取html图片(img)src地址与任意属性的方法

简单版: <?php header("Content-Type: text/html;charset=utf-8"); $str = '<div class="...

PHP读取目录树的实现方法分析

PHP读取目录树的实现方法分析

本文实例讲述了PHP读取目录树的实现方法。分享给大家供大家参考,具体如下: 前一阵时间面试XX公司笔试题中竟然有这样一道题: 使用PHP列出目录树! 当时一看就懵逼了!基本的思路还是有的...

php IP及IP段进行访问限制的代码

192.168.1.1 单个IP 192.168.1.* 这样代理 192.168.1.1-192.168.1-255 192.158.1.2-20 这样是代表192.158.1.2-1...