New to Rest API - Schedule Campaign | Community
Skip to main content
April 14, 2016
Question

New to Rest API - Schedule Campaign

  • April 14, 2016
  • 2 replies
  • 3119 views

I am trying to use the Rest API to make a call 'Schedule Campaign' call via PHP.

I have uploaded my php to my own server to call the script: (myserver.com/api-call.php).

I have set it up like they have suggested, but I keep getting the error:

{"input":{"tokens":[{"name":"{{my.TokenName}}","value":"Hello World!"}]}}{"requestId":"1428a#15412366eaf3c","success":false,"errors":[{"code":"610","message":"Requested resource not found"}]}

Any tips or tricks?

-Bryce

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

2 replies

SanfordWhiteman
Level 10
April 15, 2016

You're leaving out vital troubleshooting information, like the REST URL (hint: resource) you are connecting to.

April 18, 2016

Sanford, not 100% what you are looking for. I have basically copied what is recommended from the Schedule Campaign API page for PHP and uploaded this to one of our servers and accessed the URL. Not sure if we aren't connecting something corretly or what.

  1. <?php
  2. $request = new ScheduleCampaign();
  3. $request->id = 1001;
  4. $token1 = new stdClass();
  5. $token1->name = "{{my.token}}";
  6. $token1->value = "Hello World!";
  7. $request->tokens = array($token1);
  8. print_r($request->postData());
  9. class ScheduleCampaign{
  10.      private $host = "CHANGE ME";
  11.      private $clientId = "CHANGE ME";
  12.      private $clientSecret = "CHANGE ME";
  13.      public $id;//id of campaign to schedule
  14.      public $runAt;//dateTime to run campaign
  15.      public $cloneToProgramName;//if set will clone program with name
  16.      public $tokens;//array of stdClass objects with two members, name and value
  17.      
  18.      public function postData(){
  19.           $url = $this->host . "/rest/v1/campaigns/" . $this->id . "/schedule.json?access_token=" . $this->getToken();
  20.           $ch = curl_init($url);
  21.           $requestBody = $this->bodyBuilder();
  22.           print_r($requestBody);
  23.           curl_setopt($ch,  CURLOPT_RETURNTRANSFER, 1);
  24.           curl_setopt($ch, CURLOPT_HTTPHEADER, array('accept: application/json','Content-Type: application/json'));
  25.           curl_setopt($ch, CURLOPT_POST, 1);
  26.           curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody);
  27.           curl_getinfo($ch);
  28.           $response = curl_exec($ch);
  29.           return $response;
  30.      }
  31.      
  32.      private function getToken(){
  33.           $ch = curl_init($this->host . "/identity/oauth/token?grant_type=client_credentials&client_id=" . $this->clientId . "&client_secret=" . $this->clientSecret);
  34.           curl_setopt($ch,  CURLOPT_RETURNTRANSFER, 1);
  35.           curl_setopt($ch, CURLOPT_HTTPHEADER, array('accept: application/json',));
  36.           $response = json_decode(curl_exec($ch));
  37.           curl_close($ch);
  38.           $token = $response->access_token;
  39.           return $token;
  40.      }
  41.      private function bodyBuilder(){
  42.           $body = new stdClass();
  43.           $body->input = new stdClass();
  44.           if (isset($this->runAt)){
  45.                $body->input->runAt = $this->runAt;
  46.           }
  47.           if (isset($this->cloneToProgramName)){
  48.                $body->input->cloneToProgramName = $this->cloneToProgramName;
  49.           }
  50.           if (isset($this->tokens)){
  51.                $body->input->tokens = $this->tokens;
  52.           }
  53.           $json = json_encode($body);
  54.           return $json;
  55.      }     
  56. }

SanfordWhiteman
Level 10
April 18, 2016

These lines are intended to be replaced with your instance information:

     private $host = "CHANGE ME";

     private $clientId = "CHANGE ME";

     private $clientSecret = "CHANGE ME";

Have you gone over the REST API fundamentals doc?

Nicholas_Manojl
Level 8
April 15, 2016

The URL you are posting to is incorrect, according to the error code.

http://developers.marketo.com/documentation/rest/error-codes/