Google CSE PHP CURL实现以编程方式添加/删除注释

Google CSE PHP CURL实现以编程方式添加/删除注释,php,curl,Php,Curl,我一直在使用Googgle的定制搜索引擎API以编程方式添加和删除注释。我觉得我已经非常仔细地遵循了他们的文档和其他提示。我似乎无法避免“错误请求”HTTP 400错误。我的代码在下面 $url = "http://www.google.com/cse/api/default/annotations/"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANS

我一直在使用Googgle的定制搜索引擎API以编程方式添加和删除注释。我觉得我已经非常仔细地遵循了他们的文档和其他提示。我似乎无法避免“错误请求”HTTP 400错误。我的代码在下面

$url = "http://www.google.com/cse/api/default/annotations/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);    
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);    


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

curl_setopt($ch, CURLOPT_POST, 1);

$data = '<?xml version="1.0"?>'.chr(10);
$data .= '<Batch>'.chr(10);
$data .= chr(9).'<Add>'.chr(10);
$data .= chr(9). chr(9).'<Annotations>'.chr(10);
$data .= chr(9). chr(9). chr(9).'<Annotation about=\"\">'.chr(10);
$data .= chr(9).chr(9). chr(9). chr(9).'<Label name=\"my_engine\"/>'.chr(10);
$data .= chr(9). chr(9). chr(9).'</Annotation>'.chr(10);
$data .= chr(9). chr(9).'</Annotations>'.chr(10);
$data .= chr(9).'</Add>'.chr(10);
$data .= '</Batch>'.chr(10);    

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);    
$url=”http://www.google.com/cse/api/default/annotations/";
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$URL);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,2);
curl_setopt($ch,CURLOPT_HTTPHEADER,数组(
“授权:GoogleLogin auth='DQAAAMI…n9dGUyA'”,
“内容类型:文本/xml”
));
卷曲设置($ch,卷曲设置桩,1);
$data=''.chr(10);
$data.=''.chr(10);
$data.=chr(9)。''.chr(10);
$data.=chr(9)。第(9)条.“第(10)条”;
$data.=chr(9)。chr(9)。第(9)条.“第(10)条”;
$data.=chr(9).chr(9)。chr(9)。第(9)条.“第(10)条”;
$data.=chr(9)。chr(9)。第(9)条.“第(10)条”;
$data.=chr(9)。第(9)条.“第(10)条”;
$data.=chr(9)。''.chr(10);
$data.=''.chr(10);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
$result=curl\u exec($ch)

if(curl\u errno($ch)){
$result='cURL ERROR->'.cURL\u errno($ch)。':'.cURL\u ERROR($ch);
}否则{
$returnCode=(int)curl\u getinfo($ch,CURLINFO\u HTTP\u代码);
echo$returnCode.“
”; 交换机($returnCode){ 案例200: 打破 违约: $result='HTTP ERROR->'。$returnCode; 打破 } } 卷曲关闭($ch); 回声$结果;

如果有人能帮助我,或者至少能引导我朝着正确的方向前进,我将非常感激

好的,首先,您需要对发送到google的xml数据进行编码。其次,你应该去掉所有的标签和格式,接收引擎并不关心这些

所以试试这个:

$data = '<?xml version="1.0"?><Batch><Add><Annotations><Annotation about=\"\"><Label name=\"my_engine\"/></Annotation></Annotations></Add></Batch>';     
$data = urlencode($data);  // encode the string to send via http
$data='';
$data=urlencode($data);//对要通过http发送的字符串进行编码
我想这应该能让你走了。您可能会发现其他错误,但这将清除您得到的400错误

$data = '<?xml version="1.0"?><Batch><Add><Annotations><Annotation about=\"\"><Label name=\"my_engine\"/></Annotation></Annotations></Add></Batch>';     
$data = urlencode($data);  // encode the string to send via http