-
Make sure that the server has a valid SSL certificate installed.
-
Make sure that your version of PHP is compiled with the
--with-curl
option, as
file_get_contents()
can sometimes use cURL for HTTPS requests if it is available.
-
Try specifying the
ssl
stream context option when calling
file_get_contents()
, like this:
$context
=
stream_context_create
(
$options
);
$url
=
'https://jsonplaceholder.typicode.com/posts/1'
;
$response
=
file_get_contents
(
$url
,
false
,
$context
);
$data
=
json_decode
(
$response
,
true
);
echo
'Title: '
.
$data
[
'title'
] . PHP_EOL;
echo
'Body: '
.
$data
[
'body'
] . PHP_EOL;
Try it Yourself »
How can I prevent SQL injection in PHP?
How do I get a YouTube video thumbnail from the YouTube API?
Alternative to file_get_contents?