Programing

HTTPS에서 작동하도록 file_get_contents ()를 얻는 방법은 무엇입니까?

lottogame 2020. 8. 19. 22:30
반응형

HTTPS에서 작동하도록 file_get_contents ()를 얻는 방법은 무엇입니까?


저는 신용 카드 처리 설정을 작업 중이며 CURL에 대한 해결 방법을 사용해야했습니다. 다음 코드는 테스트 서버 (SSL URL을 호출하지 않음)를 사용할 때 제대로 작동했지만 이제 HTTPS를 사용하여 작동중인 서버에서 테스트 할 때 "스트림을 열지 못했습니다"라는 오류 메시지와 함께 실패합니다.

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);
}

다음 스크립트를 사용하여 php 스크립트에 사용할 수있는 https 래퍼가 있는지 확인하십시오.

$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);

출력은 다음과 같아야합니다.

openssl: yes
http wrapper: yes
https wrapper: yes
wrappers: array(11) {
  [...]
}

https 래퍼를 허용하려면 :

  • php_openssl확장은 존재해야하며 사용 가능
  • allow_url_fopen 로 설정되어야합니다. on

php.ini 파일에 존재하지 않는 경우 다음 행을 추가해야합니다.

extension=php_openssl.dll

allow_url_fopen = On

다음을 시도하십시오.

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.


$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


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.


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.


TO CHECK IF AN URL IS UP OR NOT

The code in your question can be rewritten as to a working version:

function send($packet=NULL, $url) {
// Do whatever you wanted to do with $packet
// The below two lines of code will let you know if https url is up or not
$command = 'curl -k '.$url;
return exec($command, $output, $retValue);
}

참고URL : https://stackoverflow.com/questions/1975461/how-to-get-file-get-contents-to-work-with-https

반응형