Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support file_get_contents() as a transport #106

Open
Roy-Orbison opened this issue Jul 23, 2020 · 1 comment
Open

Support file_get_contents() as a transport #106

Roy-Orbison opened this issue Jul 23, 2020 · 1 comment

Comments

@Roy-Orbison
Copy link

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.

@Radon8472
Copy link

Adding support for php native streams would be great, that would make this lib working without extra extensions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants