douyan9398
2012-08-20 13:36
浏览 58
已采纳

谷歌api php客户端 - Http put请求

I'm trying to create a Http put request with google-api-php-client. Oauth is working fine and I can do get requests but I just can't figure out how to do a put.

//While looping over my contacts
$apiClient = new ApiClient();
$apiClient->setAccessToken($this->Auth->user('google_oauth2_token'));

//Getting fresh XML data since I transformed mine into an array
$contactGetRequest = new apiHttpRequest($contact['link'][1]['@href'], 'GET', array('GData-Version' => 3.0));   
$rawContactData = $apiClient->getIo()->authenticatedRequest($contactGetRequest);

$contactXml = Xml::build($rawContactData->getResponseBody());

//Not changing anything, just trying to do a PUT request
$contactPutRequest = new apiHttpRequest($contact['link'][2]['@href'], 'PUT', array('GData-Version' => 3.0), $contactXml->asXML());
$response = $apiClient->getIo()->authenticatedRequest($contactPutRequest);

The error is getting is "Content-Type application/x-www-form-urlencoded is not a valid input type.".

It seems that it's trying to do a POST. What is the correct way to do a PUT request?

图片转代码服务由CSDN问答提供 功能建议

我正在尝试使用google-api-php-client创建一个Http put请求。 Oauth工作正常,我可以得到请求,但我无法弄清楚如何做一个put。

  //循环我的联系人
 $ apiClient =  new ApiClient(); 
 $ apiClient-> setAccessToken($ this-> Auth-> user('google_oauth2_token')); 
 
 //获取新的XML数据,因为我将我的数据转换为数组
  $ contactGetRequest = new apiHttpRequest($ contact ['link'] [1] ['@ href'],'GET',array('GData-Version'=> 3.0));  
 $ rawContactData = $ apiClient-> getIo() - > authenticatedRequest($ contactGetRequest); 
 
 $ contactXml = Xml :: build($ rawContactData-> getResponseBody()); 
 
 // 没有改变任何东西,只是试图做一个PUT请求
 $ contactPutRequest = new apiHttpRequest($ contact ['link'] [2] ['@ href'],'PUT',array('GData-Version'=>  3.0),$ contactXml-> asXML()); 
 $ response = $ apiClient-> getIo() - > authenticatedRequest($ contactPutRequest); 
   
 
 <  p>错误是“内容类型application / x-www-form-urlencoded不是有效的输入类型。”。   
 
 

似乎正在尝试进行POST。 执行PUT请求的正确方法是什么?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douweng3564 2012-08-20 14:03
    已采纳

    I found it. The Content-type value has to be set in the headers parameter. I tought it was done automatically.

    $contactPutRequest = new apiHttpRequest($contact['link'][2]['@href'], 'PUT', array('GData-Version' => 3.0, 'Content-type' => 'application/atom+xml; charset=UTF-8; type=entry'), $contactXml->asXML());
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题