为什么`curl_getinfo`没有返回预期的数据?

时间:2016-08-05 06:48:13

标签: php curl

我无法弄清楚为什么没有设置我的卷曲请求的内容类型。我已经减少了在控制台中工作的问题。我可能只是不理解某些事情。

php > $curl = curl_init();
php > var_dump( curl_getinfo($curl));
array(26) {
  ["url"]=>  string(0) ""
  ["content_type"]=>  NULL
  ["http_code"]=>  int(0)
  ["header_size"]=>  int(0)
  ["request_size"]=>  int(0)
  ["filetime"]=>  int(0)
  ["ssl_verify_result"]=>  int(0)
  ["redirect_count"]=>  int(0)
  ["total_time"]=>  float(0)
  ["namelookup_time"]=>  float(0)
  ["connect_time"]=>  float(0)
  ["pretransfer_time"]=>  float(0)
  ["size_upload"]=>  float(0)
  ["size_download"]=>  float(0)
  ["speed_download"]=>  float(0)
  ["speed_upload"]=>  float(0)
  ["download_content_length"]=>  float(-1)
  ["upload_content_length"]=>  float(-1)
  ["starttransfer_time"]=>  float(0)
  ["redirect_time"]=>  float(0)
  ["redirect_url"]=>  string(0) ""
  ["primary_ip"]=>  string(0) ""
  ["certinfo"]=>  array(0) {
  }
  ["primary_port"]=>  int(0)
  ["local_ip"]=>  string(0) ""
  ["local_port"]=>  int(0)
}
php > curl_setopt($curl, CURLOPT_HTTPHEADER, array('content-type: application/x-www-form-urlencoded'));
php > var_dump( curl_getinfo($curl));
array(26) {
  ["url"]=>  string(0) ""
  ["content_type"]=>  NULL
  ["http_code"]=>  int(0)
  ["header_size"]=>  int(0)
  ["request_size"]=>  int(0)
  ["filetime"]=>  int(0)
  ["ssl_verify_result"]=>  int(0)
  ["redirect_count"]=>  int(0)
  ["total_time"]=>  float(0)
  ["namelookup_time"]=>  float(0)
  ["connect_time"]=>  float(0)
  ["pretransfer_time"]=>  float(0)
  ["size_upload"]=>  float(0)
  ["size_download"]=>  float(0)
  ["speed_download"]=>  float(0)
  ["speed_upload"]=>  float(0)
  ["download_content_length"]=>  float(-1)
  ["upload_content_length"]=>  float(-1)
  ["starttransfer_time"]=>  float(0)
  ["redirect_time"]=>  float(0)
  ["redirect_url"]=>  string(0) ""
  ["primary_ip"]=>  string(0) ""
  ["certinfo"]=>  array(0) {
  }
  ["primary_port"]=>  int(0)
  ["local_ip"]=>  string(0) ""
  ["local_port"]=>  int(0)
}

只有在请求通过后才会填充此内容吗?

2 个答案:

答案 0 :(得分:-1)

在PHP CLI中,您必须使用semicolan saperated在一行中编写语句,否则$ curl的范围在下一个命令执行中过期

此外,您还没有使用curl_exec($curl);

执行卷曲
php -r '$curl = curl_init("http://www.google.com");curl_setopt($curl, CURLOPT_HTTPHEADER, array("content-type: application/x-www-form-urlencoded"));curl_exec($curl); var_dump( curl_getinfo($curl));'

答案 1 :(得分:-1)

在致电curl_exec($curl)之前,请致电curl_getinfo($curl)

请注意,"url"可能与CURLOPT_URL启用CURLOPT_FOLLOWLOCATION时设置的网址不同。这实际上命名为 CURLINFO_EFFECTIVE_URL :最后有效网址。

很遗憾,您无法检索CURLOPT_URL设置的网址。

相关问题