PHP cURL的使用方法

关于使用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)
  */
bannerAds