php - Yahoo Gemini - Making PUT request -


i'm using https://github.com/saurabhsahni/php-yahoo-oauth2/blob/master/yahoooauth2.class.php

using this example i'm able create , objects (create campaign, retrieve campaign) not able operations update , delete.

i'm getting missing fields error it's taking post call. update / delete need make put request.

so i've added in following case in yahoooauth2.class.php file

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')); } 

here complete function

 public function fetch($url, $postdata = "", $auth = "", $headers = "", $method="")     {         $curl = curl_init($url);          curl_setopt($curl, curlopt_ssl_verifypeer, false); //temporarily added disable authenticity of 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)) {             // kind of 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;     } 

when run file i'm getting following 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" }

it's working fine create , retrieve not update , delete.

here example file make update campaign.

<?php  require "yahoooauth2.class.php";   #your yahoo api consumer key & secret access 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 authorization server      header("http/1.1 302 found");     header("location: " . $oauth2client->getauthorizationurl(consumer_key,$redirect_uri));     exit; }  ?> 

any appreciated.

is there method update/delete operations or did miss above? found no example yahoo gemini documentation updating objects

finally i've ended this. in yahoooauth2.class.php file . i've added following code under fetch function.

if ($postdata)  { curl_setopt($curl, curlopt_post, true); if($method)  curl_setopt($curl, curlopt_customrequest, $method); // update , delete requests curl_setopt($curl, curlopt_postfields, $postdata); } 

it's working charm. hope helps someone


Comments