How to turn off SSL certificate validation when using PHP’s file_get_contents

Background

At times you might want to avoid certificate checking when developing a PHP application and using file_get_contents. If file_get_contents can’t work the the certificate it simply returns false making troubleshooting difficult.

Here is sample code that demonstrates how to turn SSL validation off. Included are timeout and user agent examples:

$args = [
	"ssl"  => [
		"verify_peer"      => false,
		"verify_peer_name" => false
	],
	"http" => [
		'timeout'    => 60,
		'user_agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/3.0.0.1'
		]
	];
$result = file_get_contents( $endpoint, false, stream_context_create( $args ) );

Reference

Making sure php file_get_contents accept all SSL certificates websites

Share this article

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top