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
You're leaving out vital troubleshooting information, like the REST URL (hint: resource) you are connecting to.
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.
- <?php
- $request = new ScheduleCampaign();
- $request->id = 1001;
- $token1 = new stdClass();
- $token1->name = "{{my.token}}";
- $token1->value = "Hello World!";
- $request->tokens = array($token1);
- print_r($request->postData());
- class ScheduleCampaign{
- private $host = "CHANGE ME";
- private $clientId = "CHANGE ME";
- private $clientSecret = "CHANGE ME";
- public $id;//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;
- }
- }
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?
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
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.
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/
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?
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.
*/
?>
You are duplicating the string "/rest" so the final URL ends up