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 call开发者_如何学Pythoning 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);
}
To allow https wrapper:
- the
php_openssl
extension must exist and be enabled allow_url_fopen
must be set toon
In the php.ini file you should add this lines if not exists:
extension=php_openssl.dll
allow_url_fopen = On
Try the following script to see if there is an https wrapper available for your php scripts.
$w = stream_get_wrappers();
echo 'openssl: ', extension_loaded ('openssl') ? 'yes':'no', "\n";
echo 'http wrapper: ', in_array('http', $w) ? 'yes':'no', "\n";
echo 'https wrapper: ', in_array('https', $w) ? 'yes':'no', "\n";
echo 'wrappers: ', var_export($w);
the output should be something like
openssl: yes
http wrapper: yes
https wrapper: yes
wrappers: array(11) {
[...]
}
$url= 'https://example.com';
$arrContextOptions=array(
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
);
$response = file_get_contents($url, false, stream_context_create($arrContextOptions));
This will allow you to get the content from the url whether it is a HTTPS
Try the following.
function getSslPage($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
Note: This disables SSL verification, meaning the security offered by HTTPS is lost. Only use this code for testing / local development, never on the internet or other public-facing networks. If this code works, it means the SSL certificate isn't trusted or can't be verified, which you should look into fixing as a separate issue.
This is probably due to your target server not having a valid SSL certificate.
Just add two lines in your php.ini file.
extension=php_openssl.dll
allow_url_include = On
its working for me.
I had the same error. Setting allow_url_include = On
in php.ini
fixed it for me.
HTTPS is supported starting from PHP 4.3.0, if you have compiled in support for OpenSSL.
Also, make sure the target server has a valid certificate, the firewall allows outbound connections and allow_url_fopen
in php.ini is set to true.
use the following code:
$homepage = file_get_contents("https://www.google.com",false,
stream_context_create([
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
]
])
);
echo $homepage;
In my case, the issue was due to WAMP using a different php.ini for CLI than Apache, so your settings made through the WAMP menu don't apply to CLI. Just modify the CLI php.ini and it works.
Sometimes a server will choose not to respond based on what it sees or doesn't see in the http request headers (such as an appropriate user agent). If you can connect with a browser, grab the headers it sends and mimic them in your stream context.
I was stuck with non functional https on IIS. Solved with:
file_get_contents('https.. ) wouldn't load.
- download https://curl.haxx.se/docs/caextract.html
- install under ..phpN.N/extras/ssl
edit php.ini with:
curl.cainfo = "C:\Program Files\PHP\v7.3\extras\ssl\cacert.pem" openssl.cafile="C:\Program Files\PHP\v7.3\extras\ssl\cacert.pem"
finally!
精彩评论