Git Product home page Git Product logo

http's Introduction

Http

HTTP 请求类, 支持 CURL 和 Socket, 默认使用 CURL , 当手动指定seCurl 或者 curl 扩展没有安装时, 会使用 Socket目前支持 get 和 post 两种请求方式 ,修改用于QueryList(http://querylist.cc )

Install

composer require jaeger/http

Examples

use QL\Ext\Lib\Http;
  1. 基本 get 请求:
    $http = new Http();         // 实例化对象
    $result =  $http->get('http://weibo.com/at/comment');
  1. 基本 post 请求:
    $http = new Http();         // 实例化对象
    $result = $http->post('http://someurl.com/post-new-article',array('title'=>$title, 'body'=>$body) );
  1. 模拟登录 ( post 和 get 同时使用, 利用 cookie 存储状态 ) :
    $http = new Http();         // 实例化对象
    $http->setCookiepath(substr(md5($username), 0, 10));        // 设置 cookie, 如果是多个用户请求的话
    // 提交 post 数据
    $loginData = $http->post('http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.3.19)', array('username'=>$username, 'loginPass'=>$password) );
    $result =  $http->get('http://weibo.com/at/comment');
    
  1. 利用 initialize 函数设置多个 config 信息
    $httpConfig['method']     = 'GET';
    $httpConfig['target']     = 'http://www.somedomain.com/index.html';
    $httpConfig['referrer']   = 'http://www.somedomain.com';
    $httpConfig['user_agent'] = 'My Crawler';
    $httpConfig['timeout']    = '30';
    $httpConfig['params']     = array('var1' => 'testvalue', 'var2' => 'somevalue');
    
    $http = new Http();
    $http->initialize($httpConfig);
    $http->execute();
    $result = $http->result;
  1. 复杂的设置:
    $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://yourproject.projectpath.com/login');
    
    // 开始执行请求
    $http->execute('https://yourproject.projectpath.com/login/authenticate');
    $result = $http->getResult();
  1. 获取开启了 basic auth 的请求
    $http = new Http();
    
    // Set HTTP basic authentication realms
    $http->setAuth('yourusername', 'yourpassword');
    
    // 获取某个被保护的应用的 feed
    $http->get('http://www.someblog.com/protected/feed.xml');
    
    $result = $http->result;

http's People

Contributors

jae-jae avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.