New to Rest API - Schedule Campaign

Anonymous
Not applicable

New to Rest API - Schedule Campaign

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

12 REPLIES 12
SanfordWhiteman
Level 10 - Community Moderator

Re: New to Rest API - Schedule Campaign

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

Anonymous
Not applicable

Re: New to Rest API - Schedule Campaign

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 - Community Moderator

Re: New to Rest API - Schedule Campaign

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?

Anonymous
Not applicable

Re: New to Rest API - Schedule Campaign

Yes. I have updated those. Just didn't want to post them on here .

I did go over the fundamentals and I correctly called my ID information. That might help of where I am going wrong. When doing the Schedule Campaign, should I have to call it from <REST API Endpoint URL> or can I call it from my own server.

-Bryce

SanfordWhiteman
Level 10 - Community Moderator

Re: New to Rest API - Schedule Campaign

As Nicholas and I mentioned, you're currently requesting to a URL under https://{Munchkin id}.mktorest.com/ that does not exist.  Consider it a 404.  What's the full URL you're connecting to?  Your Munchkin ID isn't secret, so feel free to include that in your post.  You can obfuscate the access_token.

Anonymous
Not applicable

Re: New to Rest API - Schedule Campaign

I am trying to call it here: http://realgoodweb.com/sandwich/ham.php

I am not sure how to call it with the necessary PHP from https://{Munchkin id}.mktorest.com/

SanfordWhiteman
Level 10 - Community Moderator

Re: New to Rest API - Schedule Campaign

Your PHP server connects to a REST API URL at Marketo. This is the only way it works.

What is the value of the PHP var $url before you make the cURL call?

Anonymous
Not applicable

Re: New to Rest API - Schedule Campaign

Sanford. Thanks for the help. here is what I am using. (I was using another variation where I had manually put in the access token on the $url line, but that is bad practice.

Here is my code:

<?php

$request = new ScheduleCampaign();

$request->id = 1001;

$token1 = new stdClass();

$token1->name = "{{my.Token}}";

$token1->value = "Hello World!";

$request->tokens = array($token1);

//$request->tokens2 = array($token2);

print_r($request->postData());

class ScheduleCampaign{

  private $host = "https://645-JLQ-943.mktorest.com/rest";

  private $clientId = "17236f28-c3bb-4bad-9762-f2e065c24eee";

  private $clientSecret = "hLsuAWkPD15mwp9Vw7ooL1nCKUBjHYS4";

    public $accessToken = "changedforprivacy1232421354:ab";

  public $id = "2639"; //id of campaign to schedule

  //public $runAt;//dateTime to run campaign

  //public $cloneToProgramName;//if set will clone program with name

  public $tokens;//array of stdClass objects with two members, name and value

   

    public function postData(){

  $url = $this->host . "/rest/v1/campaigns/" . $this->id . "/schedule.json?access_token=" . $this->getToken();

  $ch = curl_init($url);

  $requestBody = $this->bodyBuilder();

  print_r($requestBody);

  curl_setopt($ch,  CURLOPT_RETURNTRANSFER, 1);

  curl_setopt($ch, CURLOPT_HTTPHEADER, array('accept: application/json','Content-Type: application/json'));

  curl_setopt($ch, CURLOPT_POST, 1);

  curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody);

  curl_getinfo($ch);

  $response = curl_exec($ch);

  return $response;

  }

  private function getToken(){

  $ch = curl_init($this->host . "/identity/oauth/token?grant_type=client_credentials&client_id=" . $this->clientId . "&client_secret=" . $this->clientSecret);

  curl_setopt($ch,  CURLOPT_RETURNTRANSFER, 1);

  curl_setopt($ch, CURLOPT_HTTPHEADER, array('accept: application/json',));

  $response = json_decode(curl_exec($ch));

  curl_close($ch);

  $token = $response->access_token;

  return $token;

  }

  private function bodyBuilder(){

  $body = new stdClass();

  $body->input = new stdClass();

  if (isset($this->runAt)){

  $body->input->runAt = $this->runAt;

  }

  if (isset($this->cloneToProgramName)){

  $body->input->cloneToProgramName = $this->cloneToProgramName;

  }

  if (isset($this->tokens)){

  $body->input->tokens = $this->tokens;

  }

  $json = json_encode($body);

  return $json;

  }

}

/*

My code with how I am populating the email with xml data will eventually go here I assume.

*/

?>

SanfordWhiteman
Level 10 - Community Moderator

Re: New to Rest API - Schedule Campaign

You are duplicating the string "/rest" so the final URL ends up

https://645-JLQ-943.mktorest.com/rest/rest/v1/campaigns/