开发者

Google Custom Search API PHP curl Post XML HTTP 411 error

开发者 https://www.devze.com 2023-03-12 01:18 出处:网络
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 (PO

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);
  开发者_Python百科  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.


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.

$header = array('Authorization: GoogleLogin auth="'. $authToken.'"', "Content-Type: text/xml");

And always remove your Google Apps login credentials when posting your code.


Shouldn't this be [0] ? Maybe not, just a suggestion.

 $authToken = $authTokenArray[1];

Still working on figuring it out for you ....

0

精彩评论

暂无评论...
验证码 换一张
取 消