English 中文(简体)
在 PHP 中检索 Twitter s X- RateLimit- Limit
原标题:Retrieve Twitter s X-RateLimit-Limit in PHP

返回的HTTP信头应该包括X-RateLimit-Limit 。

但是,我似乎无法用PHP取回这些。 有人能告诉我我犯了什么愚蠢的错误吗?

我用正常的方式整理了卷发 并且能够成功地得到 和POST的要求。

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array( Expect: ));
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);

$response = curl_exec($ch);
$response_info=curl_getinfo($ch);
$erno = curl_errno($ch);
$er = curl_error($ch);
curl_close($ch);

我能得到一些回应信息,比如 http_code

$response_info[ http_code ]

但这条线刚刚返回无效

//Doesn t bloody work. No idea why!
$rate_limit = $response_info[ X-RateLimit-Limit ];

我正在运行PHP5.3.10版本。

<强 > EDIT

这是 print_r($response_info) 的结果 ;

Array
(
[url] => https://api.twitter.com/1/statuses/home_timeline.json...
[content_type] => application/json;charset=utf-8
[http_code] => 200
[header_size] => 695
[request_size] => 410
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 1.239977
[namelookup_time] => 0.007361
[connect_time] => 0.155783
[pretransfer_time] => 0.465397
[size_upload] => 0
[size_download] => 99425
[speed_download] => 80182
[speed_upload] => 0
[download_content_length] => 99425
[upload_content_length] => 0
[starttransfer_time] => 0.794829
[redirect_time] => 0
[certinfo] => Array()
[redirect_url] => 
[request_header] => GET /1/statuses/home_timeline.json... HTTP/1.1
Host: api.twitter.com
Accept: */*
)
最佳回答

curl_getinfo 不返回响应信头, 只有关于请求的其他元信息 。 要获取信头, 请设置 CURLOPT_ HEADER true 。 这将包括输出中的信头。 要将它们与响应机构区分开来, 需要 :

list($headers, $body) = explode("

", $response, 2);

为了分析信头,再次爆炸:

$headers = explode("
", $headers);
foreach ($headers as $header) {
    list($key, $value) = explode( : , $header, 2);
    $headers[trim($key)] = trim($value);
}

echo $headers[ X-RateLimit-Limit ];
问题回答

暂无回答




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签