当我通过
获取所有标题时$response = curl_exec($curl);
$header_size = curl_getinfo($curl,CURLINFO_HEADER_SIZE);
$result = substr($response, 0, $header_size);
我得到了像
这样的输出HTTP/1.1 302 Found
Server: nginx
Date: Mon, 06 Jul 2015 14:58:06 GMT
Content-Type: text/html;charset=utf-8
Content-Length: 0
Connection: keep-alive
Status: 302 Found
Location: https://data.myserver.com
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Request-Id: 9e2a562ec3ba73a72702e84fa7177c56
X-UA-Compatible: IE=Edge,chrome=1
Cache-Control: no-cache
X-Runtime: 0.046013
X-Rack-Cache: miss
Accept-Ranges: bytes
X-Varnish: 945594494
Age: 0
Via: 1.1 varnish
Strict-Transport-Security: max-age=31536000
我只想要'Location:'值,在Java中很容易但是在PHP中,我没有使用正则表达式找不到简单优雅的解决方案。
答案 0 :(得分:0)
也许它会对你有所帮助:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://php.net/manual/en/function.curl-getinfo.php');
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
var_export($info);
结果:
array (
'url' => 'http://php.net/manual/en/function.curl-getinfo.php',
'content_type' => 'text/html; charset=utf-8',
'http_code' => 200,
'header_size' => 596,
'request_size' => 81,
'filetime' => -1,
'ssl_verify_result' => 0,
'redirect_count' => 0,
'total_time' => 0.79269400000000001,
'namelookup_time' => 0.028351999999999999,
'connect_time' => 0.21638999999999997,
'pretransfer_time' => 0.21643999999999997,
'size_upload' => 0,
'size_download' => 62628,
'speed_download' => 79006,
'speed_upload' => 0,
'download_content_length' => -1,
'upload_content_length' => 0,
'starttransfer_time' => 0.41433799999999998,
'redirect_time' => 0,
'redirect_url' => '',
'primary_ip' => '72.52.91.14',
'certinfo' =>
array (
),
'primary_port' => 80,
'local_ip' => '192.168.12.184',
'local_port' => 36041,
)
您可以通过http://php.net/manual/en/function.curl-getinfo.php
获得$info['url']
。