Skip to content

Support file_get_contents() as a transport #106

@Roy-Orbison

Description

@Roy-Orbison

When PHP configuration option allow_url_fopen is truthy (the default), it's easy to use it as a transport. It's probably less error-prone than a raw socket, too.

$response_body = file_get_contents(
	"https://api.createsend.com/api/v3.2/${endpoint}.json"
	, false
	, stream_context_create([
		'http' => [
			'method' => 'POST',
			'header' => [
				'Authentication: Basic ' . base64_encode("${api_key}:"),
				'Content-Type: application/json',
			],
			'content' => json_encode($request_data),
			'ignore_errors' => true, # prevents returning boolean false for error http statuses
		],
	])
);
$response_headers = $http_response_header; # https://www.php.net/manual/en/reserved.variables.httpresponseheader.php

Obviously it'd need a little bit more logic to choose HTTP method, be more generic, etc., but the code's a lot simpler than cURL and raw sockets.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions