Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get file_get_contents() to work with HTTPS?

I'm working on setting up credit card processing and needed to use a workaround for CURL. The following code worked fine when I was using the test server (which wasn't calling an SSL URL), but now when I am testing it on the working server with HTTPS, it's failing with the error message "failed to open stream".

function send($packet, $url) {   $ctx = stream_context_create(     array(       'http'=>array(         'header'=>"Content-type: application/x-www-form-urlencoded",         'method'=>'POST',         'content'=>$packet       )     )   );   return file_get_contents($url, 0, $ctx); } 
like image 479
James Simpson Avatar asked Dec 29 '09 16:12

James Simpson


People also ask

What is the use of file_get_contents () function?

The file_get_contents() reads a file into a string. This function is the preferred way to read the contents of a file into a string. It will use memory mapping techniques, if this is supported by the server, to enhance performance.

What will the file_get_contents () return?

The file_get_contents() function returns Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. An E_WARNING level error is generated if filename cannot be found, maxlength is less than zero, or if seeking the specified offset in the stream fails.

Which is faster curl or file_get_contents?

curl supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading HTTP form based upload, proxies, cookies. Curl is a much faster alternative to file_get_contents.

Is file_get_contents secure?

file_get_contents in itself appears safe, as it retrieves the URL and places it into a string. As long as you're not processing the string in any script engine or using is as any execution parameter you should be safe.


1 Answers

To allow https wrapper:

  • the php_openssl extension must exist and be enabled
  • allow_url_fopen must be set to on

In the php.ini file you should add this lines if not exists:

extension=php_openssl.dll  allow_url_fopen = On 
like image 113
hassan Avatar answered Oct 16 '22 16:10

hassan