Google Custom Search API 通过 cURL 通过 php 以编程方式添加注释
我正在尝试通过使用 php 和 cURL POSTing xml 以编程方式向我的 google 自定义搜索引擎添加注释(要搜索的站点)。根据他们的开发人员文档,我需要做的就是:
POST http://www.google.com/cse/api/default/annotations/
Content-Type: text/xml
Authorization: GoogleLogin auth="IM6F7Cx2fo0TAiwlhNVdSE8Ov8hw6aHV"
<Batch>
<Add>
<Annotations>
<Annotation about="http://www.solarenergy.org/*">
<Label name="_cse_solar_example"/>
</Annotation>
<Annotation about="http://www.solarfacts.net/*">
<Label name="_cse_solar_example"/>
</Annotation>
<Annotation about="http://en.wikipedia.org/wiki/*">
<Label name="_cse_exclude_solar_example"/>
</Annotation>
</Annotations>
</Add>
</Batch>
但是,当我使用以下代码时,我收到错误 411,“POST 请求需要内容长度标头”
function getAuthorizationToken(){
$ch = curl_init();
$url = 'https://www.google.com/accounts/ClientLogin?accountType=HOSTED_OR_GOOGLE&Email=***&Passwd=***&service=cprose&source=***';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$authTokenData = curl_exec($ch);
$authTokenArray = explode('Auth=', $authTokenData);
$authToken = $authTokenArray[1];
curl_close($ch);
return $authToken;
}
$authToken = getAuthorizationToken();
$url = 'http://www.google.com/cse/api/default/annotations/';
$xml_data = '<?XML version="1.0"?>
<Batch>
<Add>
<Annotations>
<Annotation about="http://www.solarenergy.org/*">
<Label name="_cse_solar_example"/>
</Annotation>
<Annotation about="http://www.solarfacts.net/*">
<Label name="_cse_solar_example"/>
</Annotation>
<Annotation about="http://en.wikipedia.org/wiki/*">
<Label name="_cse_exclude_solar_example"/>
</Annotation>
</Annotations>
</Add>
</Batch>';
$length = strlen($xml_data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_HTTPHEADER, Array("Authorization: GoogleLogin auth=". $authToken, "Content-Type: text/xml", "Content-Length: " . $length));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // ask for results to be returned
$result = curl_exec($ch);
curl_close($ch);
echo $result;
我已经在这个问题上挣扎了很长时间时间,我将非常感谢任何帮助。
谢谢
I'm trying to add annotations (sites to search) to my google custom search engine programmatically by POSTing xml using php and cURL. According to their developer documentation, all I need to do is:
POST http://www.google.com/cse/api/default/annotations/
Content-Type: text/xml
Authorization: GoogleLogin auth="IM6F7Cx2fo0TAiwlhNVdSE8Ov8hw6aHV"
<Batch>
<Add>
<Annotations>
<Annotation about="http://www.solarenergy.org/*">
<Label name="_cse_solar_example"/>
</Annotation>
<Annotation about="http://www.solarfacts.net/*">
<Label name="_cse_solar_example"/>
</Annotation>
<Annotation about="http://en.wikipedia.org/wiki/*">
<Label name="_cse_exclude_solar_example"/>
</Annotation>
</Annotations>
</Add>
</Batch>
However, when I use the following code, I get an error 411, "POST requests require a Content-length header"
function getAuthorizationToken(){
$ch = curl_init();
$url = 'https://www.google.com/accounts/ClientLogin?accountType=HOSTED_OR_GOOGLE&Email=***&Passwd=***&service=cprose&source=***';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$authTokenData = curl_exec($ch);
$authTokenArray = explode('Auth=', $authTokenData);
$authToken = $authTokenArray[1];
curl_close($ch);
return $authToken;
}
$authToken = getAuthorizationToken();
$url = 'http://www.google.com/cse/api/default/annotations/';
$xml_data = '<?XML version="1.0"?>
<Batch>
<Add>
<Annotations>
<Annotation about="http://www.solarenergy.org/*">
<Label name="_cse_solar_example"/>
</Annotation>
<Annotation about="http://www.solarfacts.net/*">
<Label name="_cse_solar_example"/>
</Annotation>
<Annotation about="http://en.wikipedia.org/wiki/*">
<Label name="_cse_exclude_solar_example"/>
</Annotation>
</Annotations>
</Add>
</Batch>';
$length = strlen($xml_data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_HTTPHEADER, Array("Authorization: GoogleLogin auth=". $authToken, "Content-Type: text/xml", "Content-Length: " . $length));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // ask for results to be returned
$result = curl_exec($ch);
curl_close($ch);
echo $result;
I've been struggling with this issue for a very long time, and I would greatly appreciate any help with this.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将注释发送到
http://www.google.com/cse/api/default/annotations/solar_example
并从 curl 选项中删除Content-Length
标头。它应该看起来像这样:它对我有用。
Try to send annotations to
http://www.google.com/cse/api/default/annotations/solar_example
and removeContent-Length
header from curl options. It should looks like this:It works for me.