【问题标题】:Yahoo Gemini - Making PUT requestYahoo Gemini - 发出 PUT 请求
【发布时间】:2015-07-17 11:33:30
【问题描述】:

我正在使用https://github.com/saurabhsahni/php-yahoo-oauth2/blob/master/YahooOAuth2.class.php

使用this example 我可以创建和获取对象(创建活动、检索活动),但不能执行更新删除等操作。 p>

我收到了缺少字段的错误,因为它把它当作 POST 调用。对于更新/删除,我需要提出 PUT 请求。

为此,我在 YahooOAuth2.class.php 文件中添加了以下情况

if($method) 
{
curl_setopt($curl, CURLOPT_PUT, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: PUT'));
}

这是完整的功能

 public function fetch($url, $postdata = "", $auth = "", $headers = "", $method="")
    {
        $curl = curl_init($url);

        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //Temporarily added to disable authenticity of the peer's certificate 

        if ($postdata) {
             if($method) 
             {  
             curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
             curl_setopt($curl, CURLOPT_PUT, true);      
             curl_setopt($curl, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: PUT'));
             }
            curl_setopt($curl, CURLOPT_POST, true);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
        } else {
            curl_setopt($curl, CURLOPT_POST, false);
        }
        if ($auth) {
            curl_setopt($curl, CURLOPT_USERPWD, $auth);
        }
        if ($headers) {
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        }
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($curl);
        if (empty($response)) {
            // some kind of an error happened
            die(curl_error($curl));
            curl_close($curl); // close cURL handler
        } else {
            $info = curl_getinfo($curl);
            curl_close($curl); // close cURL handler
            if ($info['http_code'] != 200 && $info['http_code'] != 201) {
                echo "Received error: " . $info['http_code']. "\n";
                echo "Raw response:".$response."\n";
                die();
            }
        }
        return $response;
    }

当我运行文件时,我得到以下响应

object(stdClass)#2 (3) { ["errors"]=> array(1) { [0]=> object(stdClass)#3 (4) { ["errIndex"]=> int(- 1) ["code"]=> string(28) "E10000_INTERNAL_SERVER_ERROR" ["message"]=> string(12) "invalid JSON" ["description"]=> string(0) "" } } ["response" ]=> NULL ["timestamp"]=> string(18) "2015-07-20 6:21:14" }

createretrieve 工作正常,但 updatedelete 却不行。

这是制作更新广告系列的示例文件。

<?php

require "YahooOAuth2.class.php"; 

#Your Yahoo API consumer key & secret with access to Gemini data 

define("CONSUMER_KEY","sdfsadfw23r23423rsdf--");
define("CONSUMER_SECRET","234234sdfwr");
$redirect_uri="http://".$_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'];
$gemini_api_endpoint="https://api.admanager.yahoo.com/v1/rest";
//$gemini_api_endpoint="https://sandbox-api.admanager.yahoo.com/v1/rest";

$oauth2client=new YahooOAuth2();

if (isset($_GET['code'])){
    $code=$_GET['code'];    
} 
else {
    $code=0;
}

if($code){
     $token=$oauth2client->get_access_token(CONSUMER_KEY,CONSUMER_SECRET,$redirect_uri,$code);
     $headers= array('Authorization: Bearer '.$token,'Accept: application/json','Content-Type: application/json');
     $url=$gemini_api_endpoint."/campaign/";
    $data = array("id"=>12356,"budget"=> 500);  
     $postdata = json_encode($data);
     $method = "PUT";
     $resp=$oauth2client->fetch($method, $url,$postdata,$auth="",$headers);
      $jsonResponse = json_decode( $resp);
      var_dump($jsonResponse);
}
else {
    # no valid access token available, go to authorization server 
    header("HTTP/1.1 302 Found");
    header("Location: " . $oauth2client->getAuthorizationURL(CONSUMER_KEY,$redirect_uri));
    exit;
}

?>

任何帮助将不胜感激。

是否有任何方法可以进行更新/删除操作,或者我错过了上面的任何内容?我没有从 Yahoo Gemini Documentation 中找到更新对象的示例

【问题讨论】:

    标签: php yahoo-api


    【解决方案1】:

    我终于变成了这样。在 YahooOAuth2.class.php 文件中。我刚刚在 fetch 函数下添加了以下代码。

    if ($postdata) 
    {
    curl_setopt($curl, CURLOPT_POST, true);
    if($method)  curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); // For Update and Delete requests
    curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
    }
    

    它就像一个魅力。我希望它可以帮助某人

    【讨论】:

      【解决方案2】:

      颠倒语句的顺序,因为最后一个 CURLOPT_POSTFIELDS 暗示 CURLOPT_POST,正如这里的文档 http://curl.haxx.se/libcurl/c/CURLOPT_POSTFIELDS.html 中所指出的那样。因此,您需要使用 PUT 覆盖它,如下所示:

      if($method) 
      {
        curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
        curl_setopt($curl, CURLOPT_PUT, 1);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: PUT'));
      }
      

      【讨论】:

      • 感谢您的回答。我已经改变了它,但仍然得到同样的错误。我错过了什么吗?
      • 我收到以下响应 >object(stdClass)#2 (3) { ["errors"]=> array(1) { [0]=> object(stdClass)#3 ( 4) { ["errIndex"]=> int(-1) ["code"]=> string(28) "E10000_INTERNAL_SERVER_ERROR" ["message"]=> string(12) "invalid JSON" ["description"]= > string(0) "" } } ["response"]=> NULL ["timestamp"]=> string(18) "2015-07-20 6:21:14" }
      • 看来你的数据无效;现在身份验证正常
      • 是的,我重新检查了我的输入数据,我提供了正确的 JSON 格式并传递了所需的参数。不知道发生了什么事。例如,要更新活动,我将发送有效的现有 ID。
      • 在 PUT 中,您通常必须提供完整的对象内容,而不仅仅是要修改的元素
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-06
      • 2020-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-10
      相关资源
      最近更新 更多