Giter VIP home page Giter VIP logo

cakephp-requests's Introduction

CakePHP Requests

Requests is a replacement for file_get_contents function for external urls and uses curl package for creating requests.

IMPORTANT: You need curl extension installed and enabled in your php.ini configuration file.

Install Requests

  • Create Utility folder in src if doesn't exists.
  • Copy the Requests.php and RequestsInterface.php files from src > Utility to your Utility folder.

Requirements

  • PHP >= 7.1.x
  • cURL extension enabled
  • CakePHP >= 3.6.x

Composer

Add following statement in require area.

{
    // ...
    "require": {
        "ext-curl": "*",
    }
    // ...
}

Allowed methods

    // GET
    Requests::get(string $url, array $context = []);
    // POST
    Requests::post(string $url, array $context = []);
    // PUT
    Requests::put(string $url, array $context = []);
    // PATCH
    Requests::patch(string $url, array $context = []);
    // DELETE
    Requests::delete(string $url, array $context = []);

Context options and types

  • fields: [a=>b, c=>d] or 'a=b&c=d' or null
  • user_password: 'user:password' or null
  • headers: ['type'=>'value', 'type:value'] or []
  • proxy: null
  • proxy_type: 'http' or see below
  • timeout: 0 (seconds)
  • connection_timeout: 0 (seconds)
  • ssl_verify: false

Proxy

Requests has a proxy method included:

    $proxyObject = Requests::proxy('https://username:password@hostname:9090/');

    echo $proxyObject->scheme;
    echo $proxyObject->host;
    echo $proxyObject->port;
    echo $proxyObject->username;
    echo $proxyObject->password;
    echo $proxyObject->proxy;

You can also use proxy context key in request and proxy_type:

Proxy types:

  • http (default)
  • socks4
  • socks5

Usage:

    $result = Requests::post('https://github.com/', [
        'proxy' => 'http://username:password@hostname:9090/',
        'proxy_type' => 'http'
    ]);

    echo $result->getErrorNumber();
    echo $result->getErrorMessage();

Example

Load Requests package and make a request. Get response code and full output as result.

namespace App\Controller;

use App\Utility\Requests;
use Cake\Controller\Controller;

class AppController extends Controller
{
    // Your code
    public function index()
    {
        $result = \App\Utility\Requests::get('https://github.com/', [
            'timeout' => 10,
            'connection_timeout' => 10
        ]);

        echo $result->getHttpResponseCode();
        echo $result->getOutput();
    }
    // Your code
}

Enjoy ;)

cakephp-requests's People

Contributors

mrred85 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.