PHP CURL 및 HTTPS
AWESOME 작업 (IMHO)을 수행하는이 함수를 찾았습니다. http://nadeausoftware.com/articles/2007/06/php_tip_how_get_web_page_using_curl
/**
* Get a web file (HTML, XHTML, XML, image, etc.) from a URL. Return an
* array containing the HTTP server response header fields and content.
*/
function get_web_page( $url )
{
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "spider", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
);
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );
$header['errno'] = $err;
$header['errmsg'] = $errmsg;
$header['content'] = $content;
return $header;
}
내가 가진 유일한 문제는 https : //에서 작동하지 않는다는 것입니다. Anny가 https에서이 작업을 수행하기 위해해야 할 일을 생각합니까? 감사!
빠른 수정, 옵션에 다음을 추가하십시오.
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false)
이제 cURL은 어떤 방식 으로든 인증서를 확인하지 않기 때문에 실제로 연결중인 호스트를 알 수 없습니다. man-in-the-middle 공격 을 즐기시기 바랍니다 !
또는 현재 기능에 추가하십시오.
/**
* Get a web file (HTML, XHTML, XML, image, etc.) from a URL. Return an
* array containing the HTTP server response header fields and content.
*/
function get_web_page( $url )
{
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "spider", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_SSL_VERIFYPEER => false // Disabled SSL Cert checks
);
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );
$header['errno'] = $err;
$header['errmsg'] = $errmsg;
$header['content'] = $content;
return $header;
}
CURL을 사용하여 php로 https API 호출을 수행하려고 시도했지만이 문제가 발생했습니다. 나는 나를 시작하고 실행하는 PHP 사이트에서 권장 사항을 발견했습니다 : http://php.net/manual/en/function.curl-setopt.php#110457
Please everyone, stop setting CURLOPT_SSL_VERIFYPEER to false or 0. If your PHP installation doesn't have an up-to-date CA root certificate bundle, download the one at the curl website and save it on your server:
http://curl.haxx.se/docs/caextract.html
Then set a path to it in your php.ini file, e.g. on Windows:
curl.cainfo=c:\php\cacert.pem
Turning off CURLOPT_SSL_VERIFYPEER allows man in the middle (MITM) attacks, which you don't want!
Another option like Gavin Palmer answer is to use the .pem
file but with a curl option
download the last updated
.pem
file from https://curl.haxx.se/docs/caextract.html and save it somewhere on your server(outside the public folder)set the option in your code instead of the
php.ini
file.
In your code
curl_setopt($ch, CURLOPT_CAINFO, $_SERVER['DOCUMENT_ROOT'] . "/../cacert-2017-09-20.pem");
NOTE: setting the cainfo in the php.ini
like @Gavin Palmer did is better than setting it in your code like I did, because it will save a disk IO every time the function is called, I just make it like this in case you want to test the cainfo file on the fly instead of changing the php.ini
while testing your function.
One important note, the solution mentioned above will not work on local host, you have to upload your code to server and then it will work. I was getting no error, than bad request, the problem was I was using localhost (test.dev,myproject.git). Both solution above work, the solution that uses SSL cert is recommended.
Go to https://curl.haxx.se/docs/caextract.html, download the latest cacert.pem. Store is somewhere (not in public folder - but will work regardless)
Use this code
".$result; //echo "
Path:".$_SERVER['DOCUMENT_ROOT'] . "/ssl/cacert.pem"; // this is for troubleshooting only ?>
- Upload the code to live server and test.
참고URL : https://stackoverflow.com/questions/4372710/php-curl-https
'development' 카테고리의 다른 글
Guava를 사용하여 컬렉션을 변환하는 동안 null을 제거하는 우아한 방법이 있습니까? (0) | 2020.12.07 |
---|---|
변경 사항을 확인하려면 nodejs를 다시 시작하지 않고 서버 파일을 편집하려면 어떻게해야합니까? (0) | 2020.12.07 |
배열이 비어 있지 않은지 확인하는 방법은 무엇입니까? (0) | 2020.12.07 |
VIM에서 다음 N 문자 변경 (0) | 2020.12.07 |
Dart에서 런타임 유형 검사를 수행하는 방법은 무엇입니까? (0) | 2020.12.07 |