欢迎访问WDPHP素材源码!今天是2024年03月28日 星期四,下午好!别打盹哦!
您好,游客 [ 马上登录 | 注册帐号 | 微信登录 | QQ登录]
当前位置:首页 > 源码 > 函数/类库 > PHP HTTP操作类 , 支持 Curl 和 Socket

PHP HTTP操作类 , 支持 Curl 和 Socket
栏目分类:函数/类库    发布日期:2017-12-22    浏览次数:4985次     收藏

TTP 请求类, 支持 CURL 和 Socket, 默认使用 CURL , 当手动指定 useCurl 或者 curl 扩展没有安装时, 会使用 Socket 目前支持 get 和 post 两种请求方式
浏览次数:4985 文件大小:7.87KB 下载积分:20积分

1. 基本 get 请求:

    $http = new Http();         // 实例化对象
    $result =  $http->get('http://www.xxx.com/comment');

2. 基本 post 请求:

    $http = new Http();         // 实例化对象
    $result = $http->post('http://www.xxx.com/post-new-article', array('title'=>$title, 'body'=>$body) );

3. 模拟登录 ( post 和 get 同时使用, 利用 cookie 存储状态 ) :

    $http = new Http();         // 实例化对象
    $http->setCookiepath(substr(md5($username), 0, 10));        // 设置 cookie, 如果是多个用户请求的话
    // 提交 post 数据
    $loginData = $http->post('http://www.xxx.com/sso/login.php?client=ssologin.js(v1.3.19)', array('username'=>$username, 'loginPass'=>$password) );
    $result =  $http->get('http://weibo.com/at/comment');

4. 利用 initialize 函数设置多个 config 信息

    $httpConfig['method']     = 'GET';
    $httpConfig['target']     = 'http://www.wdphp.com.com/index.html';
    $httpConfig['referrer']   = 'http://www.wdphp.com.com';
    $httpConfig['user_agent'] = 'My Crawler';
    $httpConfig['timeout']    = '30';
    $httpConfig['params']     = array('var1' => 'testvalue', 'var2' => 'somevalue');
    
    $http = new Http();
    $http->initialize($httpConfig);
    
    $result = $http->result;

5. 复杂的设置:

    $http = new Http();
    $http->useCurl(false);      // 不使用 curl
    $http->setMethod('POST');       // 使用 POST method
    
    // 设置 POST 数据
    $http->addParam('user_name' , 'yourusername');
    $http->addParam('password'  , 'yourpassword');
    
    // Referrer
    $http->setReferrer('https://www.xxx.com/login');
    
    // 开始执行请求
    $http->execute('https://www.xxx.com/login/authenticate');
    $result = $http->getResult();

6. 获取开启了 basic auth 的请求

  
    $http = new Http();
    
    // Set HTTP basic authentication realms
    $http->setAuth('yourusername', 'yourpassword');
    
    // 获取某个被保护的应用的 feed
    $http->get('http://www.wdphp.com/protected/feed.xml');
    
    $result = $http->result;

申明:本站代码不提供任何技术服务,仅提供有偿服务。WDPHP素材源码所有资源都经过检测,请放心下载!

相关热词: HTTPCurlSocket

源码 模板 特效 素材 资源 教程 站长