Coder Social home page Coder Social logo

requests's Introduction

requests

介绍

PHP Requests 支持 GET、POST、PUT、PATCH、DELETE、HEAD、OPTIONS 等方法

运行环境

  • PHP版本 ( >= 5.4 )
  • composer

安装

composer require ihexiang/requests v1.0.1

支持的方法

响应 Response

  • 每一个请求都返回一个响应对象 response

  • 成员属性

    • header 响应头信息
    • http_code HTTP code 码
    • body 响应内容体
    • erron 错误码
    • error 错误消息
  • 成员方法

    • content() 获取文本类型的 响应内容
    • toArray(dataType) 获取数组类型的 响应内容

示例

<?php
use iHexiang\Requests\Requests;

    //1.简单示例
    echo Requests::get('https://xxx.example/test.api');


    //2.返回响应示例
    $response = Requests::get('https://xxx.example/test.api');
    if(!$response->http_code){
        echo $response->error;
    }else{

        //注:以下2种效果是一样的
        echo $response;
        echo $response->content();

        //打印数组
        //注:接口必须返回 json 或 xml 格式数据
        //var_dump($response->toArray('json'));
        //var_dump($response->toArray('xml'));
    }

    //3.全部字段使用示例
    $response = Requests::get(
        'https://xxx.example/test.api',
        ['Content-Type: text/html;charset=utf-8'],
        ['timeout'=>10]
    );


    if(!$response->http_code){
        echo $response->error;
    }else{

        //注:以下2种效果是一样的
        echo $response;
        echo $response->content();

        //打印数组
        //注:接口必须返回 json 或 xml 格式数据
        //var_dump($response->toArray('json'));
        //var_dump($response->toArray('xml'));
    }
    
   

LICENSE

Apache-2.0

requests's People

Contributors

ihexiang avatar

Watchers

James Cloos 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.