0
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?

More than 3 years have passed since last update.

PHP cURLの使い方

Last updated at Posted at 2021-04-02

curlを使用する一連のコードの流れについて

参考:
Qiita
Refect

基本

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.hogehoge.com/");
//(その他用途に応じてcurl_setopt()を記述)
curl_exec($ch);
curl_close($ch);

上記を実行するとhogehoge.comのサイトページの情報を取得し、表示出来る。

curl_getinfoについて

curl_getinfoはリクエストに関する情報を配列として返す。
使用する場所:curl_exec()とcurl_close()の間に記述する。

上記を踏まえてグーグルの情報を取得する例

<?php 


/* curlセッションを初期化する */
$ch = curl_init();

/* curlオプションを設定する */
curl_setopt($ch, CURLOPT_URL, "http://www.google.com/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);


/* curlを実行する */
$result = curl_exec($ch);

$info = curl_getinfo($ch);


var_dump($info);


/* curlセッションを終了する */
curl_close($ch);


//var_dump($info)の結果は以下
/*
array(26) {
  ["url"]=>
  string(22) "http://www.google.com/"
  ["content_type"]=>
  string(29) "text/html; charset=ISO-8859-1"
  ["http_code"]=>
  int(200)
  ["header_size"]=>
  int(732)
  ["request_size"]=>
  int(53)
  ["filetime"]=>
  int(-1)
  ["ssl_verify_result"]=>
  int(0)
  ["redirect_count"]=>
  int(0)
  ["total_time"]=>
  float(0.127532)
  ["namelookup_time"]=>
  float(0.005124)
  ["connect_time"]=>
  float(0.01553)
  ["pretransfer_time"]=>
  float(0.015613)
  ["size_upload"]=>
  float(0)
  ["size_download"]=>
  float(49800)
  ["speed_download"]=>
  float(390490)
  ["speed_upload"]=>
  float(0)
  ["download_content_length"]=>
  float(-1)
  ["upload_content_length"]=>
  float(-1)
  ["starttransfer_time"]=>
  float(0.105488)
  ["redirect_time"]=>
  float(0)
  ["redirect_url"]=>
  string(0) ""
  ["primary_ip"]=>
  string(14) "216.58.197.196"
  ["certinfo"]=>
  array(0) {
  }
  ["primary_port"]=>
  int(80)
  ["local_ip"]=>
  string(13) "192.168.0.101"
  ["local_port"]=>
  int(55635)
  */
0
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
0
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?