Google Custom Search API 通过 cURL 通过 php 以编程方式添加注释

发布于 2024-11-25 01:23:08 字数 2499 浏览 1 评论 0原文

我正在尝试通过使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叹沉浮 2024-12-02 01:23:08

尝试将注释发送到 http://www.google.com/cse/api/default/annotations/solar_example 并从 curl 选项中删除 Content-Length 标头。它应该看起来像这样:

curl_setopt ($ch, CURLOPT_HTTPHEADER, Array(
    "Authorization: GoogleLogin auth=". $authToken, 
    "Content-Type: text/xml"
));

它对我有用。

Try to send annotations to http://www.google.com/cse/api/default/annotations/solar_example and remove Content-Length header from curl options. It should looks like this:

curl_setopt ($ch, CURLOPT_HTTPHEADER, Array(
    "Authorization: GoogleLogin auth=". $authToken, 
    "Content-Type: text/xml"
));

It works for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文