|
| 1 | +<?php |
| 2 | +/* Example code to access Gemini API: Fetch advertiser information, create a new campaign and read specific campaign data*/ |
| 3 | + |
| 4 | +require "YahooOAuth2.class.php"; #Download here: https://github.com/saurabhsahni/php-yahoo-oauth2/ |
| 5 | + |
| 6 | +/*Your Yahoo API consumer key & secret with access to Gemini data */ |
| 7 | + |
| 8 | +define("CONSUMER_KEY","<your consumer key>"); |
| 9 | +define("CONSUMER_SECRET","<your consumer secret>"); |
| 10 | +$redirect_uri="http://".$_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'];//Or your other redirect URL - must match the callback domain |
| 11 | + |
| 12 | +$gemini_api_endpoint="https://api.admanager.yahoo.com/v1/rest"; |
| 13 | + |
| 14 | +$oauth2client=new YahooOAuth2(); |
| 15 | + |
| 16 | +if (isset($_GET['code'])){ |
| 17 | + $code=$_GET['code']; |
| 18 | +} |
| 19 | +else { |
| 20 | + $code=0; |
| 21 | +} |
| 22 | + |
| 23 | +if($code){ |
| 24 | + #oAuth 3-legged authorization is successful, fetch access token |
| 25 | + $token=$oauth2client->get_access_token(CONSUMER_KEY,CONSUMER_SECRET,$redirect_uri,$code); |
| 26 | + |
| 27 | + #access token is available. Do API calls. |
| 28 | + |
| 29 | + $headers= array( |
| 30 | + 'Authorization: Bearer '.$token, |
| 31 | + 'Accept: application/json', |
| 32 | + 'Content-Type: application/json' |
| 33 | + ); |
| 34 | + |
| 35 | + //Fetch Advertiser Name and Advertiser ID |
| 36 | + $url=$gemini_api_endpoint."/advertiser/"; |
| 37 | + |
| 38 | + $resp=$oauth2client->fetch($url,$postdata="",$auth="",$headers); |
| 39 | + $jsonResponse = json_decode( $resp); |
| 40 | + $advertiserName = $jsonResponse->response[0]->advertiserName; |
| 41 | + $advertiserId = $jsonResponse->response[0]->id; |
| 42 | + echo "Welcome ".$advertiserName; |
| 43 | + |
| 44 | + //Create a new campaign |
| 45 | + $url=$gemini_api_endpoint."/campaign"; |
| 46 | + $postdata='{ |
| 47 | + "status":"PAUSED", |
| 48 | + "campaignName":"NativeAdsCampaign", |
| 49 | + "budget": 3000, |
| 50 | + "budgetType": "LIFETIME", |
| 51 | + "advertiserId": '.$advertiserId.', |
| 52 | + "channel":"NATIVE" |
| 53 | + }'; |
| 54 | + |
| 55 | + $resp=$oauth2client->fetch($url,$postdata=$postdata,$auth="",$headers); |
| 56 | + $jsonResponse = json_decode( $resp); |
| 57 | + |
| 58 | + $campaignID=$jsonResponse->response->id; |
| 59 | + $campaignName=$jsonResponse->response->campaignName; |
| 60 | + |
| 61 | + echo "\n<br>Created a new campaign with ID: ".$campaignID; |
| 62 | + |
| 63 | + //Read specific campaign data |
| 64 | + $url=$gemini_api_endpoint."/campaign/".$campaignID; |
| 65 | + $resp=$oauth2client->fetch($url,$postdata="",$auth="",$headers); |
| 66 | + $jsonResponse = json_decode( $resp); |
| 67 | + echo "\n<br> Campaign object:<br>\n"; |
| 68 | + print_r($jsonResponse->response); |
| 69 | +} |
| 70 | +else { |
| 71 | + /* no valid access token available, go to authorization server */ |
| 72 | + header("HTTP/1.1 302 Found"); |
| 73 | + header("Location: " . $oauth2client->getAuthorizationURL(CONSUMER_KEY,$redirect_uri)); |
| 74 | + exit; |
| 75 | +} |
| 76 | + |
| 77 | +?> |
| 78 | + |
0 commit comments