Google 自定义搜索 API PHP curl 发布 XML HTTP 411 错误
我尝试了几种不同的方法,使用 xml 发送 POST 请求,以将注释添加到我的 google 自定义搜索中。我尝试过的每种不同方法都会导致 HTTP 411 错误(POST 请求需要 Content-length 标头)。这是我当前的代码:
<?php
$ch = curl_init();
$url = 'https://www.google.com/accounts/ClientLogin?accountType=HOSTED_OR_GOOGLE&Email=****&Passwd=*****&service=cprose&source=ednovo-classadmin-1';
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];
echo "got token: $authToken<br>";
$annotation = $_POST['annotation'];
$label = $_POST['label'];
$href = $_POST['href'];
curl_close($ch);
$data ='<Batch>' .
'<Add>' .
'<Annotations>' .
'<Annotation about = "' . $annotation . '">' .
'<Label name = "' . $label . '" />' .
'</Annotation>' .
'</Annotations>' .
'</Add>' .
'</Batch>';
$url = "http://www.google.com/cse/api/default/annotations/";
curl_setopt($ch, CURLOPT_URL, $url);
$length = strlen($data);
$header = array("Authorization: GoogleLogin auth=" . $authToken, "Content-Type: text/xml","Content-Length: " . $length);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
if ( curl_errno($ch) ) {
$result = 'cURL ERROR -> ' . curl_errno($ch) . ': ' . curl_error($ch);
} else {
$returnCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
switch($returnCode){
case 200:
break;
default:
$result = 'HTTP ERROR -> ' . $returnCode;
break;
}
}
echo $result;
?>
我感谢任何帮助。
谢谢。
我感谢您的帮助,因为这是我的问题之一。但是,即使在使用 init 并更改标头之后,它仍然给我相同的 HTTP 411 错误。您还有其他想法吗?非常感谢。
I have tried several different methods of sending a POST request with my xml to add an annotation to my google custom search. Each different method that I've tried has resulted in a HTTP 411 error (POST requests require a Content-length header). Here is my current code:
<?php
$ch = curl_init();
$url = 'https://www.google.com/accounts/ClientLogin?accountType=HOSTED_OR_GOOGLE&Email=****&Passwd=*****&service=cprose&source=ednovo-classadmin-1';
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];
echo "got token: $authToken<br>";
$annotation = $_POST['annotation'];
$label = $_POST['label'];
$href = $_POST['href'];
curl_close($ch);
$data ='<Batch>' .
'<Add>' .
'<Annotations>' .
'<Annotation about = "' . $annotation . '">' .
'<Label name = "' . $label . '" />' .
'</Annotation>' .
'</Annotations>' .
'</Add>' .
'</Batch>';
$url = "http://www.google.com/cse/api/default/annotations/";
curl_setopt($ch, CURLOPT_URL, $url);
$length = strlen($data);
$header = array("Authorization: GoogleLogin auth=" . $authToken, "Content-Type: text/xml","Content-Length: " . $length);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
if ( curl_errno($ch) ) {
$result = 'cURL ERROR -> ' . curl_errno($ch) . ': ' . curl_error($ch);
} else {
$returnCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
switch($returnCode){
case 200:
break;
default:
$result = 'HTTP ERROR -> ' . $returnCode;
break;
}
}
echo $result;
?>
I appreciate any help with this.
Thank you.
I appreciate your help as this was one of my problems. However, it is still giving me the same HTTP 411 error even after using the init and changing the header. Do you have any other ideas? Thanks a bunch.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在第一个 ClientLogin 请求后调用了
curl_close($ch);
,但忘记为第二个请求再次执行$ch = curl_init();
。添加您不需要放置 Content-Length 标头字段;设置帖子字段/数据时,curl 会自动填充此内容。您还可以尝试在“授权”字段中的 auth 值上添加引号。在发布代码时,请务必删除您的 Google Apps 登录凭据。
You called
curl_close($ch);
after the first request for ClientLogin, but you forgot to do a$ch = curl_init();
again for the second request. Add you don't need to put the Content-Length header field; curl populates this automatuically when setting the post fields/data. You could also try adding quotes on the auth value in the Authorization field.And always remove your Google Apps login credentials when posting your code.
这不应该是 [0] 吗?也许不是,只是一个建议。
仍在努力为您解决......
Shouldn't this be [0] ? Maybe not, just a suggestion.
Still working on figuring it out for you ....