title: Yahoo Gemini - Making PUT request
body: <p>I'm using  <a href="https://github.com/saurabhsahni/php-yahoo-oauth2/blob/master/YahooOAuth2.class.php" rel="nofollow">https://github.com/saurabhsahni/php-yahoo-oauth2/blob/master/YahooOAuth2.class.php</a></p>
  
  <p>Using <a href="https://developer.yahoo.com/gemini/#get-started=" rel="nofollow">this example</a> I'm able to create and get Objects (create campaign, retrieve campaign) but not able to do Operations like <em>Update</em> and <em>delete</em>.</p>
  
  <p>I'm getting missing fields error as it's taking it as POST call. For Update / delete I need to make PUT request.</p>
  
  <p>So for that I've added in the following case in YahooOAuth2.class.php file </p>
  
  <pre><code>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'));
  }
  </code></pre>
  
  <p>Here is the complete function</p>
  
  <pre><code> 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 &amp;&amp; $info['http_code'] != 201) {
                  echo "Received error: " . $info['http_code']. "\n";
                  echo "Raw response:".$response."\n";
                  die();
              }
          }
          return $response;
      }
  </code></pre>
  
  <p>When I run the file I'm getting the following response</p>
  
  <blockquote>
    <p>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" } </p>
  </blockquote>
  
  <p>It's working fine for <strong>create</strong> and <strong>retrieve</strong> but not for <strong>update</strong> and <strong>delete</strong>.</p>
  
  <p>Here is the example file to make Update Campaign. </p>
  
  <pre><code>&lt;?php
  
  require "YahooOAuth2.class.php"; 
  
  #Your Yahoo API consumer key &amp; 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-&gt;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"=&gt;12356,"budget"=&gt; 500);  
       $postdata = json_encode($data);
       $method = "PUT";
       $resp=$oauth2client-&gt;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-&gt;getAuthorizationURL(CONSUMER_KEY,$redirect_uri));
      exit;
  }
  
  ?&gt;
  </code></pre>
  
  <p>Any help would be much appreciated. </p>
  
  <p>Is there any method to do Update/delete operations or did I miss anything from above? I found no example from <a href="https://developer.yahoo.com/gemini" rel="nofollow">Yahoo Gemini Documentation</a> for Updating Objects</p>