2月 132016
 

PHP cURLの色々な使い方を参考にcURLで接続をこころみたものの、すぐつまづいて、ドはまりしたので、メモ。

以下の部分、file_get_contents()なら取得できるのに、cURLで取得できない。

$url = "http://www.pixiv.net/login.php";
$ch = curl_init(); // はじめ

//オプション
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$html = curl_exec($ch);
var_dump($html);
curl_close($ch); //終了

このURLがリダイレクトされるので、リダイレクト後のアドレスに変えてみる。

https://www.secure.pixiv.net/login.php?return_to=0

エラーをはきださせる。

curl_errnoなどを使って、直前のcurl_exec()のエラーを出力。

if($errno = curl_errno($ch)) {
  $error_message = curl_strerror($errno);
  echo "cURL error ({$errno}):\n {$error_message}";
}

curl: (60) Peer certificate cannot be authenticated with known CA certificates

サーバー証明書の検証がうまくいっていないために、データが取得できないようだ。

cURL はサーバー証明書の検証を行わないように設定

setoptに、以下のように設定。
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

これでhttpsのデータも取得できるようになった。

よく読まれている記事

この記事を読んだ人は次の記事も読んでいます:

このエントリーをはてなブックマークに追加
はてなブックマーク - file_get_contents()なら取得できるのにCurlでうまくいかない
[`google_buzz` not found]
[`yahoo` not found]
[`livedoor` not found]
[`friendfeed` not found]
[`tweetmeme` not found]
[`grow` not found]
[`evernote` not found]

 Posted by at 4:23 PM  Tagged with:

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)