【问题标题】:Google Custom Search API adding annotations programmatically through php via cURL谷歌自定义搜索 API 通过 cURL 通过 php 以编程方式添加注释
【发布时间】:2011-07-15 17:05:42
【问题描述】:

我正在尝试通过使用 php 和 cURL 发布 xml 以编程方式将注释(要搜索的站点)添加到我的谷歌自定义搜索引擎。根据他们的开发者文档,我需要做的就是:

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 请求需要 Content-length 标头”

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;

我已经为这个问题苦苦挣扎了很长时间,如果能提供任何帮助,我将不胜感激。

谢谢

【问题讨论】:

    标签: php xml curl google-custom-search


    【解决方案1】:

    尝试将注释发送到 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"
    ));
    

    它对我有用。

    【讨论】:

    • 你有没有改过别的?不幸的是,根据您的建议更改了我的代码后,它仍然对我不起作用
    • 不,我没有。其他一切似乎都一样。
    猜你喜欢
    • 1970-01-01
    • 2016-09-06
    • 2011-10-15
    • 2013-01-21
    • 1970-01-01
    • 1970-01-01
    • 2012-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多