5
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

CURLOPT_SSL_VERIFYPEERについて

Posted at

はじめに

  • CURLOPT_SSL_VERIFYPEERについて備忘録。

内容

CURLOPT_SSL_VERIFYPEER

  • HTTPSなどのTLS接続で、相手サーバー証明書の真正性を検証するかどうかを切り替えるオプション
  • デフォルトはtrue
  • falseにすると、証明書の検証を停止する。無効は推奨されていないので注意

証明書検証を無効にすると、libcurl が悪意のあるサーバーからの情報を信頼して使用してしまう可能性があります。

処理の流れ

  • 大体以下のように実行するみたい。
xxx.php
<?php
$url = 'https://api.example.com/items';

// 1) セッション初期化(ハンドルを受け取る)
$ch = curl_init($url);

// 2) オプション設定
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true, // 文字列として返す
    CURLOPT_TIMEOUT        => 10,   // 全体タイムアウト
    CURLOPT_CONNECTTIMEOUT => 5,    // 接続タイムアウト
    CURLOPT_SSL_VERIFYPEER => true, // 証明書検証 有効
    CURLOPT_SSL_VERIFYHOST => 2,    // ホスト名検証
    CURLOPT_HTTPHEADER     => [
        'Accept: application/json',
        'User-Agent: my-app/1.0',
    ],
]);

// 3) 実行
$body = curl_exec($ch);

// 4) エラー/レスポンス確認
if ($body === false) {
    $err = curl_error($ch);
    curl_close($ch);
    throw new RuntimeException("cURL error: {$err}");
}
$code = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);

// 5) 終了
curl_close($ch);

まとめ

  • CURLOPT_SSL_VERIFYPEERは必ずtrueを基本にする。証明書エラーになる場合は、システム上のCA証明書が不足しているか、古くなっていることが原因のことが多いので、そちらで対応する方が良さそう。

参考記事

5
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?