php 使用 curl 详解

1.   创建一个 curl 链接,以百度为例

   // 初始化一个curl会话资源; 

   $ch = curl_init(); 


   // 设置URL

   curl_setopt($ch, CURLOPT_URL, "www.baidu.com"); 

 
   //这是设置是否将响应结果存入变量,1是存入,0是直接输出

   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

 
   // 执行,并将响应结果存入$output变量 

   $output = curl_exec($ch); 


    //输出

    echo $output;

 
   // 关闭资源释放内存空间

   curl_close($ch); 

2.   post 请求  数组

   $ch = curl_init(); 

    curl_setopt($ch, CURLOPT_URL, "http://www.地址.com/******.php"); 

    curl_setopt($ch, CURLOPT_POST, 1);        // 使用post请求

    //设置最长连接时间,秒为单位.

    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60); 

    curl_setopt($ch, CURLOPT_POSTFIELDS , http_build_query($data));   //$data为你要传输的数据,这里是数组 使用 http_build_query 处理

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

    $output = curl_exec($ch); 

    echo $output;

    curl_close($ch);  

3.  post 请求  json 数据

  $ch = curl_init(); 

    curl_setopt($ch, CURLOPT_URL, "http://地址/*****.php"); 

    curl_setopt($ch, CURLOPT_POST, 1);

    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60); 

    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length:' . strlen($data)));   //数据为json格式

    curl_setopt($ch, CURLOPT_POSTFIELDS , $data);   //$data 为 json 数据

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

    $output = curl_exec($ch); 

    echo $output;

    curl_close($ch); 

4.  post  上传图片

    $data = ['name'=>'boy', "upload"=>""];

    $ch = curl_init(); 

    $data['upload']=new CURLFile(realpath(getcwd().'/boy.png'));   //设置图片路径

    curl_setopt($ch, CURLOPT_URL, "http://地址/*******.php");

    curl_setopt($ch, CURLOPT_POST, 1);

    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60); 

    curl_setopt($ch, CURLOPT_POSTFIELDS , http_build_query($data));  //数组处理

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

    $output = curl_exec($ch); 

    echo $output;

    curl_close($ch); 

5.  post 获取 图片内容

   $ch = curl_init(); 

   $fp=fopen('./girl.jpg', 'w');   // 写入方式打开图片,文件同理

   curl_setopt($ch, CURLOPT_URL, "http://地址/***.jpg"); 

   curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60); 

   curl_setopt($ch, CURLOPT_FILE, $fp); 

    $output = curl_exec($ch); 

    $info = curl_getinfo($ch);   //获取本次请求相关信息

    fclose($fp);     //关闭

    $size = filesize("./girl.jpg");

    if ($size != $info['size_download']) {

        echo "下载的数据不完整,请重新下载";

    } else {

        echo "下载数据完整";

    }
curl_close($ch);

6.  http 验证

$ch = curl_init();

    curl_setopt_array($ch, [

        CURLOPT_USERPWD => $user.':'.$password,   //$user用户名,$password密码

        CURLOPT_URL     => ' URL 地址 ',

        CURLOPT_RETURNTRANSFER => true

    ]);

    $result = curl_exec($ch);

    curl_close($ch);

7.cookie  模拟登陆

//模拟登录  

function login_post($url, $cookie, $post) { 

    $curl = curl_init();

    curl_setopt($curl, CURLOPT_URL, $url);

    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 0);

    curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie);

    curl_setopt($curl, CURLOPT_POST, 1);

    curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));

    curl_exec($curl); 

    curl_close($curl);

}


//登录成功后获取数据  

function get_content($url, $cookie) { 

    $ch = curl_init(); 

    curl_setopt($ch, CURLOPT_URL, $url); 

    curl_setopt($ch, CURLOPT_HEADER, 0);   //CURLLOPT_HEADER设置为0表示不返回HTTP头部信息。

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); 

    $rs = curl_exec($ch); 

    curl_close($ch); 

    return $rs; 

}



//设置post的数据    begin

  $post = array ( 

    'email' => '账户', 

    'pwd' => '密码'

  ); 

  //登录地址  

  $url = "登陆地址";  

  //设置cookie保存路径  

  $cookie = dirname(__FILE__) . '/cookie.txt';  

  //登录后要获取信息的地址  

  $url2 = "登陆后要获取信息的地址";  

  //模拟登录 

  login_post($url, $cookie, $post);  

  //获取登录页的信息  

  $content = get_content($url2, $cookie);  

  //删除cookie文件 

  @ unlink($cookie);

  var_dump($content);

8. curl 携带ip进行访问

$url="地址";
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_HEADER, 0 );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );
curl_setopt($ch, CURLOPT_HTTPHEADER , array('X-FORWARDED-FOR:***.***.***.***', 'CLIENT-IP:***.***.***.***'));  //构造IP 
$pcontents = curl_exec ($ch);

 

 

 

 

 

CURL的选项还有很多,可以到PHP的官方网站(http://www.php.net/manual/en/function.curl-setopt.php)上查看CURL支持的所有选项列表。

文章转载自  http://www.php.cn/php-weizijiaocheng-393305.html

posted @ 2018-11-08 12:00  晨曦时丶梦见兮  阅读(220)  评论(0)    收藏  举报