php的curl实现get和post的代码

yipeiwu_com6年前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 Stream_*系列函数

PHP Stream_*系列函数

下图是对这个系列函数的思维导图——我也是第一次使用思维导图这种工具。大图在这里。其中stream_socket_*系列是PHP 5新加入的处理socket连接的函数,简单方便,下一篇博客...

php遍历目录下文件并按修改时间排序操作示例

本文实例讲述了php遍历目录下文件并按修改时间排序操作。分享给大家供大家参考,具体如下: php 遍历目录下文件方法 //遍历目录下文件方法 function printdir($d...

php查找任何页面上的所有链接的方法

使用DOM,你可以轻松从任何页面上抓取链接,代码示例如下: 复制代码 代码如下: $html = file_get_contents('http://www.example.com');...

php处理单文件、多文件上传代码分享

php处理  单文件、多文件上传实例代码,供大家参考,具体内容如下  后台处理文件submit_form_process.php  <?...

php中执行系统命令的方法

本文实例讲述了php中执行系统命令的方法。分享给大家供大家参考。具体分析如下: 在php中执行系统命令,如LS <?php // exec.php $cmd = "di...