PHP code example of ihexiang / requests

1. Go to this page and download the library: Download ihexiang/requests library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ihexiang / requests example snippets

 

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'));
    }