Tsugi

Net
in package

Outbound HTTP using the PHP curl extension.

GET and POST (and other body methods) require ext-curl. There is no stream or socket fallback.

Table of Contents

Properties

$VERIFY_PEER  : mixed

Methods

bodyCurl()  : mixed
doBody()  : mixed
doGet()  : mixed
ensureUserAgentHeader()  : string
Ensure a header string includes a User-Agent line.
getBodyReceivedDebug()  : mixed
getBodySentDebug()  : mixed
getCurl()  : mixed
getGetReceivedDebug()  : mixed
getGetSentDebug()  : mixed
getIP()  : string
Get the actual IP address of the incoming request.
getLastBODYContent()  : mixed
getLastBODYDebug()  : mixed
getLastBODYImpl()  : mixed
getLastBODYMethod()  : mixed
getLastBODYURL()  : mixed
getLastCurlError()  : mixed
getLastGETImpl()  : mixed
getLastGETURL()  : mixed
getLastHeadersReceived()  : mixed
getLastHeadersSent()  : mixed
getLastHttpResponse()  : mixed
getUserAgent()  : string
Build the User-Agent string used for outbound HTTP requests.
httpSuccess()  : mixed
Return true if the http code is 2xx (success)
isRoutable()  : mixed
Return true if we have a routable address
parseHeaders()  : mixed
Extract a set of header lines into an array
send400()  : mixed
Send a 400 (Malformed request) header
send403()  : mixed
Send a 403 header
serverIP()  : mixed
Return the IP Address of the current server
setCookieStrong()  : mixed
Put as much oomph into setting a cookie as we can
setUserAgentCurl()  : void
Set the User-Agent header on a cURL handle.
requireCurl()  : void

Properties

$VERIFY_PEER

public static mixed $VERIFY_PEER = false

Methods

bodyCurl()

public static bodyCurl(mixed $url, mixed $method, mixed $body, mixed $header) : mixed
Parameters
$url : mixed
$method : mixed
$body : mixed
$header : mixed

doBody()

public static doBody(mixed $url, mixed $method, mixed $body, mixed $header) : mixed
Parameters
$url : mixed
$method : mixed
$body : mixed
$header : mixed

doGet()

public static doGet(mixed $url[, mixed $header = false ]) : mixed
Parameters
$url : mixed
$header : mixed = false

ensureUserAgentHeader()

Ensure a header string includes a User-Agent line.

public static ensureUserAgentHeader([string|false $header = false ]) : string
Parameters
$header : string|false = false

Existing headers (newline-separated) or false

Return values
string —

Headers including User-Agent

getBodyReceivedDebug()

public static getBodyReceivedDebug() : mixed

getBodySentDebug()

public static getBodySentDebug() : mixed

getCurl()

public static getCurl(mixed $url[, mixed $header = false ]) : mixed
Parameters
$url : mixed
$header : mixed = false

getGetReceivedDebug()

public static getGetReceivedDebug() : mixed

getGetSentDebug()

public static getGetSentDebug() : mixed

getIP()

Get the actual IP address of the incoming request.

public static getIP() : string

Handle being behind a load balancer or a proxy like Cloudflare. This will often return NULL when talking to localhost to make sure to test code using this on a real IP address.

Adapted from: https://www.chriswiegman.com/2014/05/getting-correct-ip-address-php/ With some additional explode goodness via: http://stackoverflow.com/a/25193833/1994792

Return values
string —

The IP address of the incoming request or NULL if it cannot be determined.

getLastBODYContent()

public static getLastBODYContent() : mixed

getLastBODYDebug()

public static getLastBODYDebug() : mixed

getLastBODYImpl()

public static getLastBODYImpl() : mixed

getLastBODYMethod()

public static getLastBODYMethod() : mixed

getLastBODYURL()

public static getLastBODYURL() : mixed

getLastCurlError()

public static getLastCurlError() : mixed

getLastGETImpl()

public static getLastGETImpl() : mixed

getLastGETURL()

public static getLastGETURL() : mixed

getLastHeadersReceived()

public static getLastHeadersReceived() : mixed

getLastHeadersSent()

public static getLastHeadersSent() : mixed

getLastHttpResponse()

public static getLastHttpResponse() : mixed

getUserAgent()

Build the User-Agent string used for outbound HTTP requests.

public static getUserAgent() : string

Canvas and other LMS platforms reject requests without a User-Agent (PHP file_get_contents sends none / empty and gets HTTP 403). Override via the CFG extension mechanism in your config.php file:

$CFG->setExtension('user_agent', 'MyTool/1.0 Tsugi/25.05');

The default looks like this, using TSUGI_VERSION from setup.php:

Tsugi/26.11.5 (https://www.py4e.com/tsugi) PHP/8.4.1
Return values
string

httpSuccess()

Return true if the http code is 2xx (success)

public static httpSuccess(mixed $httpcode) : mixed
Parameters
$httpcode : mixed

isRoutable()

Return true if we have a routable address

public static isRoutable(mixed $ipaddr) : mixed
Parameters
$ipaddr : mixed

parseHeaders()

Extract a set of header lines into an array

public static parseHeaders([mixed $headerstr = false ]) : mixed

Takes a newline separated header string and returns a key/value array. Keys are kept as received (HTTP/2 and HTTP/3 send them lowercase). Header names are case-insensitive (RFC 7230 / RFC 9110), so look them up with U::getIgnoreCase() rather than U::get().

Parameters
$headerstr : mixed = false

send400()

Send a 400 (Malformed request) header

public static send400([mixed $msg = 'Malformed request' ][, mixed $detail = null ]) : mixed
Parameters
$msg : mixed = 'Malformed request'
$detail : mixed = null

send403()

Send a 403 header

public static send403([mixed $msg = null ][, mixed $detail = null ]) : mixed
Parameters
$msg : mixed = null
$detail : mixed = null

serverIP()

Return the IP Address of the current server

public static serverIP() : mixed

setCookieStrong()

Put as much oomph into setting a cookie as we can

public static setCookieStrong(string $name, string $value, string $domain, int $expires[, bool $partitioned = true ]) : mixed
Parameters
$name : string

Cookie name

$value : string

Cookie value

$domain : string

Cookie domain

$expires : int

Unix timestamp for expiration

$partitioned : bool = true

Add Partitioned attribute (CHIPS) for iframe/third-party use

setUserAgentCurl()

Set the User-Agent header on a cURL handle.

public static setUserAgentCurl(resource $ch) : void
Parameters
$ch : resource

A cURL handle

requireCurl()

private static requireCurl() : void

        
On this page

Search results