Code Monkey home page Code Monkey logo

url-status's Introduction

url-status

Run Tests License Latest Stable Version Latest Unstable Version

Passing the url returns the status by looking at the header information

Inspired by https://secure.php.net/manual/ja/function.get-headers.php#119497

Installation

$ composer require shimabox/url-status

Usage

<?php
require_once 'vendor/autoload.php';

$ret = \SMB\UrlStatus::get($url);

$ret->targetUrl();      // string 対象のURL
$ret->reachedUrl();     // string 最後に到達したURL
$ret->isValidUrl();     // bool   有効なURLか
$ret->redirectedUrls(); // array  リダイレクトがあった際のURL
$ret->redirectedCode(); // array  リダイレクト時のHTTPステータスコード
$ret->code();           // int    最終的なHTTPステータスコード

// is{数値3桁}() でマジックメソッドが呼ばれます
$ret->is200();          // bool   HTTPステータスコードが200かどうか
$ret->is401();          // bool   HTTPステータスコードが401かどうか
$ret->is403();          // bool   HTTPステータスコードが403かどうか
$ret->is404();          // bool   HTTPステータスコードが404かどうか
$ret->is405();          // bool   HTTPステータスコードが405かどうか
$ret->is500();          // bool   HTTPステータスコードが500かどうか
$ret->is503();          // bool   HTTPステータスコードが503かどうか
.
.
$ret->isxxx();          // /\Ais(\d{3})\z/ will call the magic method

Example

<?php
require_once 'vendor/autoload.php';

$ret = \SMB\UrlStatus::get('https://google.com/webhp?gl=us&hl=en&gws_rd=cr');

$ret->targetUrl();      // => 'https://google.com/webhp?gl=us&hl=en&gws_rd=cr'
$ret->reachedUrl();     // => 'https://www.google.com/webhp?gl=us&hl=en&gws_rd=cr'
$ret->isValidUrl();     // => true
$ret->redirectedUrls(); // => ['https://www.google.com/webhp?gl=us&hl=en&gws_rd=cr']
$ret->redirectedCode(); // => [301]
$ret->code();           // => 200
$ret->is200();          // => true

// for the POST method
$data = http_build_query(['foo' => 'bar', 'hoge' => 'piyo']);
$header = [
    'Content-Type: application/x-www-form-urlencoded',
    'Content-Length: ' . strlen($data)
];
$options = [
    'http' => [
        'method'  => 'POST',
        'header'  => implode("\r\n", $header),
        'content' => $data
    ]
];

$ret = \SMB\UrlStatus::get('http://localhost/post.php', $options);

Testing

$ composer test

or

$ vendor/bin/phpunit

License

The MIT License (MIT). Please see License File for more information.

url-status's People

Contributors

shimabox avatar kleidp avatar

Watchers

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