Google API PHP客户端:发送etag [英] Google API php client: send etag
问题描述
我正在使用最新的 Google PHP客户端库.如何通过此库发送etag?
I'm using the latest Google PHP Client library. How can I send the etag with this library?
我已经尝试过这样的事情:
I've already tried something like this:
$opt_params = array(
'etag' => 'F9iA7pnxqNgrkOutjQAa9F2k8HY/mOihMVEDLCvdSVCzwDmYHpSV');
$response = $youtube->channels->listChannels('snippet', array('id' => $channel_id), $opt_params);
谢谢!
推荐答案
The Google API PHP Client does not have native support for etags. That said, you can still alter the curl request and apply an etag as a header.
不幸的是,当Google_Http_REST
收到304 Not Modified
标头时会引发异常,因此您需要在catch
-block中单独处理这些标头.
Unfortunately Google_Http_REST
throws an exception when it receives a 304 Not Modified
header so you'll need to handle those seperately in a catch
-block.
这是生成的PHP代码(请注意,这是针对Google API PHP客户端的版本1):
This is the resulting PHP-code (note, this is for version 1 of the Google API PHP Client):
class EmptyResponse { }
$DEVELOPER_KEY = '';
$client = new Google_Client();
$client->setDeveloperKey($DEVELOPER_KEY);
$youtube = new Google_Service_YouTube($client);
// Do some logic, catch some resources
// ..
// Set the "If-None-Match" header for the etag
// This assumes the Google API is using CURL
$client->getIo()->setOptions(array(
CURLOPT_HTTPHEADER => array('If-None-Match: "4FSIjSQU83ZJMYAO0IqRYMvZX98/OPaxOhv3eeKc9owSnDQugGfL8ss"'),
));
try {
$response = $youtube->playlists->listPlaylists('snippet', array(
'id' => 'PLOIvOnfHWjIkiz6fd5KYUXJY6ZpHRqPfW',
));
}
catch (Google_Service_Exception $e) {
if (304 == $e->getCode()) {
// If the error code is 304 (Not Modified) return an empty response
$response = new EmptyResponse;
}
else {
// The request was unsuccesful
$response = null;
}
}
// After the request set the headers back to default
$client->getIo()->setOptions(array(
CURLOPT_HTTPHEADER => array(),
));
var_dump($response);
// Catch some more resources
// ...
这篇关于Google API PHP客户端:发送etag的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!