Не могу через php и curl сделать POST запрос через https.
Запрос http работает без проблем.
Запрос http работает без проблем.
31.10.2018 17:20:56
Не могу через php и curl сделать POST запрос через https.
Запрос http работает без проблем. |
|
|
31.10.2018 17:29:45
Собственно параметры CURL для работы с SSL:
CURLOPT_SSL_VERIFYPEER — если поставить его в 0, то удалённый сервер не будет проверять наш сертификат. В противном случае необходимо этот самый сертификат послать. CURLOPT_CAINFO — указывать файл сертификата, если CURLOPT_SSL_VERIFYPEER установлен в 1. CURLOPT_SSLVERSION — целое число, указывает версию SSL (2 или 3), обычно определяется автоматически. CURLOPT_SSL_VERIFYHOST — будет ли производиться проверка имени удалённого сервера, указанного в сертификате. Если установить значение «2», то будет произведена ещё и проверка соответствия имени хоста. (если честно, я так и не понял что делает этот флаг) И самый простой способ CURLOPT_SSL_VERIFYPEER поставить в 0 ПРИМЕР: $postfields = array('field1'=>'value1', 'field2'=>'value2'); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://foo.com'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_POST, 1); // Edit: prior variable $postFields should be $postfields; curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // On dev server only! $result = curl_exec($ch); Значение остальных параметров: CURLOPT_URL — это URL запроса. CURLOPT_POST — говорим, что будем посылать POST запрос. CURLOPT_POSTFIELDS — собственно POST переменыые. CURLOPT_RETURNTRANSFER — вернуть результат запроса, а не выводить в браузер. |
|
|
31.10.2018 17:48:32
А вот что написано в хелпе:
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: 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! Что означет не использовать, тот метод что я описал выше CURLOPT_SSL_VERIFYPEER, а обновить CA root certificate bundle и прописать путь к сертификату: curl.cainfo Если вы хотите посмотреть как выглядит HTTP \ HTTPS запрос который вы отправляете в браузере, то поставьте в chrome расширение Advanced REST client |
|
|
31.10.2018 18:23:15
Еще небольшой пример как работать curl впринципи в php:
/** * 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; } This sample function takes a URL argument and returns an associative array containing the server header and content. The CURL functions automatically handle DNS lookups, redirects, cookies, and file decompression. Header values are described in the curl_getinfo() manual. Highlights include: "url" the final page URL after redirects "content_type" the content type (e.g. "text/html; charset=utf-8") "http_code" the page status code (e.g. "200" on success) "filetime" the date stamp on the remote file This function adds:: "errno" the CURL error number (0 on success) "errmsg" the CURL error message for the error number "content" the page content (e.g. HTML text, image bytes, etc.) On success, "errno" is 0, "http_code" is 200, and "content" contains the web page. On an error with a bad URL, unknown host, timeout, or redirect loop, "errno" has a non-zero error code and "errmsg" has an error message (see the CURL error code list). On an error with a missing web page or insufficient permissions, "errno" is 0, "http_code" has a non-200 HTTP status code, and "content" contains the site’s error message page (see the Wikipedia List of HTTP status codes). This function can be extended to support GET and POST for web forms, file uploads, logins, SSL for encrypted web pages, and access through proxy servers. |
||||
|
||||