How do i add a lead (email, name) against a list in Marketo via API - marketo

Marketo API is so confusing for a new user like myself. I have an email and name and would love to pass that to marketo. How do I do that?

To push a lead record to Marketo via the REST API, there are a couple of endpoints you can choose from:
Sync Leads at POST /rest/v1/leads.json
Push Lead at POST /rest/v1/leads/push.json
Import Leads at POST /bulk/v1/leads.json
Out of these three, the Sync Leads might be the easiest to use, as that requires the least amout of additional parameters.
Basically, you have to make a POST request to the
https://<MUNCHKIN_ID>.mktorest.com/rest/v1/leads.json?access_token=<ACCESS_TOKEN> url with your data sent in the body of the request.
You will find your MUNCHKIN_ID under Admin > Integration > Munchkin tab in your instance. While still in the admin area, you should also create an API user (or allow API access for your own user), set up a LaunchPoint service with that user for the REST API and finally request an –temporary, valid for 1 hour– access token to test the connection. The whole process is described in detail under the Authentication chapter of the REST API documentation.
Once you have a –still valid– access token, you can make the call above with your lead information provided in the following data structure:
{
"action":"createOrUpdate",
"lookupField":"email",
"input":[
{
"email":"collizo#4sky.com",
"firstName":"Collizo4sky"
},
// …more leads (up to 300) if needed
]
}
In case you use php, here is an example code:
$munchkinId = '123-ABC-456';
$accessToken = 'abcdefgh-1234-5678-abcd-12345678abcd:lon';
$url = "https://{$munchkinId}.mktorest.com/rest/v1/leads.json?access_token={$accessToken}";
$dataJSON = [
'action' => 'createOrUpdate',
'lookupField' => 'email',
'input' => [
[
'email' => 'collizo#4sky.com',
'firstName' => 'Collizo4sky',
],
],
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($dataJSON));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
/**
* Which should result in a response like this:
* /
{
"requestId":"4033#16612d185ad",
"result":[
{
"id":1042016,
"status":"created"
}
],
"success":true
}
/**/

Related

How to convert curl call with “-i --upload-file” into PHP?

This call is referenced in step 2 of the docs here
I am following the docs to create an image and text share in LinkedIn through the version 2 API. Following the steps in the docs, I completed each step with a successful response. However, my post does not exist in LinkedIn, and cannot be viewed by direct link based on the ID returned by the API request
In debuging I made a get request to https://api.linkedin.com/v2/assets/C5622AQFbyxMfU2b9zg. This is to retrieve the media asset (docs here) created in the first step. It returned the following json with the 'CLIENT_ERROR' status. If you look at the docs though, this status is not one of those listed for the field
{
"serviceRelationships": [
{
"identifier": "urn:li:userGeneratedContent",
"relationshipType": "OWNER"
}
],
"recipes": [
{
"recipe": "urn:li:digitalmediaRecipe:feedshare-image",
"status": "CLIENT_ERROR"
}
],
"mediaTypeFamily": "STILLIMAGE",
"created": 1550875198973,
"lastModified": 1550875199857,
"id": "C5622AQFbyxMfU2b9zg",
"status": "ALLOWED"
}
I was able to successfully upload the file using the command line curl. It seems the issue is with my PHP implementation. Specifically the upload of the binary file is not working right. Here is a simplified representation of my php code.
$ch = curl_init();
$headers = [
'Authorization: Bearer ' . $this->accessToken->access_token,
'Cache-Control: no-cache',
'X-RestLi-Protocol-Version: 2.0.0',
'x-li-format: json',
'Content-Type: ' . $this->mimetype
];
$data = [
'file' => curl_file_create($filename, $this->mimetype);
];
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_URL, 'https://api.linkedin.com/mediaUpload/C5622AQHSLBWkZeg-gQ/feedshare-uploadedImage/0?ca=vector_feedshare&cn=uploads&m=AQJIFvVUxXl7yAAAAWkwjT0kEPKHY5BnAgXETcVQ3AbsxmvaGl6hnuECwA&app=22961836&sync=0&v=beta&ut=2RaKRdHD_OwUE1');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
print_r($result);
I ran into this same issue recently and linkedin's documents were not very helpful.
Create an Image Share.
The first thing to understand is that "curl --upload-file" uses a PUT request method. Here is what I used using dummy data.
$file = "/storage/media/testimg.jpg";
$uploadurl = "https://api.linkedin.com/mediaUpload/C5622AQHSLBWkZeg-gQ/feedshare-uploadedImage/0?ca=vector_feedshare&cn=uploads&m=AQJIFvVUxXl7yAAAAWkwjT0kEPKHY5BnAgXETcVQ3AbsxmvaGl6hnuECwA&app=22961836&sync=0&v=beta&ut=2RaKRdHD_OwUE1";
$accesstoken = "acacdsv13rv31bv13b1cf13cf13rv13rv13vr13tb5dxvghn";
$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $uploadurl);
curl_setopt($curl_handle, CURLOPT_PUT, 1);
curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, "PUT");
$fh_res = fopen($file, 'r');
curl_setopt($curl_handle, CURLOPT_INFILE, $fh_res);
curl_setopt($curl_handle, CURLOPT_INFILESIZE, filesize($file));
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
$headers[] = "Authorization: Bearer $accesstoken"; //linkedin says to add 'redacted' instead of access token (that's bullshit)
$headers[] = 'Connection: Keep-Alive';
$headers[] = "X-RestLi-Protocol-Version:2.0.0";
curl_setopt($curl_handle,CURLOPT_HTTPHEADER,$headers);
$rcurl = curl_exec($curl_handle);
curl_close($curl_handle);
If you dump the response $rcurl you only get an empty string. So the next step is to check the status of the upload, something they completely forgot to mention as part of the image share documentation but can be found here Check Status of Uphold
Using the asset variable from the initial image register call, you check the upload's status until the status check returns AVAILABLE.
$asset = "urn:li:digitalmediaAsset:C4asdfvqqrv3";
$a = explode("digitalmediaAsset:", $asset);
$statuscheck = make a GET curl call to the url: "/assets/" . $a[1]
$uploadstat = $statuscheck['recipes'][0]['status'];
do{
$statuscheck = make a GET curl call to the url: "/assets/" . $a[1];
$uploadstat = $statuscheck['recipes'][0]['status'];
}while($uploadstat != 'AVAILABLE');
Once AVAILABLE is returned, you can now proceed to the next step of creating the image share. Hope this helps.

woocommerce subscription API

I am using Woocomemrce REST API to connect with my site. Everything works fine when it comes to orders but it is not working with subscriptions. I have tried following code to get subscriptions but it is giving "Error: No route was found matching the URL and request method [rest_no_route]"
$woocommerce = new Client(
'https://www.example.com',
'ck_OUR_CONSUMER_KEY',
'cs_OUR_CONSUMER_SECRET',
[
'wp_api' => true,
'version' => 'wc/v2',
]
);
try {
print_r($woocommerce->get('orders')); //this works and fetch orders
print_r($woocommerce->get('subscriptions')); //but this does not work
} catch (HttpClientException $e) {
echo $e->getMessage(); // Error message.
echo $e->getRequest(); // Last request data.
echo $e->getResponse(); // Last response data.
}
Can anyone help me sort out this issue. Thank You.
I changed it to the following it worked for me.
$woocommerce = new Client(
'https://www.example.com',
'ck_OUR_CONSUMER_KEY',
'cs_OUR_CONSUMER_SECRET',
[
'wp_api' => true,
'version' => 'wc/v1',
]
);

Symfony2, Swiftmailer, Mandrill get response

We've build an email system using Swiftmailer and Mandrill. It works really great but now we would like to integrate the webhooks to trigger alerts on bounces / failures / ...
At this moment we add a custom header with an unique id to each email sent and find our way back when the webhook triggers.
It works well but mandrill already uses an _id that we could use so that we do not add 'another' unique ID on top of that.
Mandrill responds with something like:
[
{
"email": "recipient.email#example.com",
"status": "sent",
"reject_reason": "hard-bounce",
"_id": "abc123abc123abc123abc123abc123"
}
]
Is there any way in Swiftmailer to get this response back into Symfony ?
(So that we can read and store that _id for later use)
I know we could use the Mandrill php SDK but preferably we would like to keep on using Swiftmailer.
EDIT
We are using SMTP Transport with a basic Swiftmailer instance as explained here
<?php
include_once "swift_required.php";
$subject = 'Hello from Mandrill, PHP!';
$from = array('you#yourdomain.com' =>'Your Name');
$to = array(
'recipient1#example.com' => 'Recipient1 Name',
'recipient2#example2.com' => 'Recipient2 Name'
);
$text = "Mandrill speaks plaintext";
$html = "<em>Mandrill speaks <strong>HTML</strong></em>";
$transport = Swift_SmtpTransport::newInstance('smtp.mandrillapp.com', 587);
$transport->setUsername('MANDRILL_USERNAME');
$transport->setPassword('MANDRILL_PASSWORD');
$swift = Swift_Mailer::newInstance($transport);
$message = new Swift_Message($subject);
$message->setFrom($from);
$message->setBody($html, 'text/html');
$message->setTo($to);
$message->addPart($text, 'text/plain');
if ($recipients = $swift->send($message, $failures))
{
echo 'Message successfully sent!';
} else {
echo "There was an error:\n";
print_r($failures);
}
?>
I don't think Mandrill supports passing this reply via SMTP, you'll have to use API for that.
For example, in accord/mandrill-swiftmailer there is a method that returns response from Mandrill: https://github.com/AccordGroup/MandrillSwiftMailer/blob/master/SwiftMailer/MandrillTransport.php#L215
You can get Mandrill's response using following code:
$transport = new MandrillTransport($dispatcher);
$transport->setApiKey('ABCDEFG12345');
$transport->setAsync(true); # Optional
$response = $transport->getMandrillMessage($message);
// $response now contains array with Mandrill's response.
You can integrate it with Symfonu using accord/mandrill-swiftmailer-bundle and after that you can do:
$response = $mailer->getTransport()->getMandrillMessage($message);

HTTP client Cakephp 3 ignores json body

I'm currently writing a RESTful API in Cakephp 3 whereby I need to test a POST operation through http://host.com/api/pictures. The code for the test:
<?php
namespace App\Test\TestCase\Controller;
use App\Controller\Api\UsersController;
use Cake\TestSuite\IntegrationTestCase;
use Cake\Network\Http\Client;
use Cake\Network\Http\FormData;
class ApiPicturesControllerTest extends IntegrationTestCase{
public $fixtures = [
'app.users',
'app.comments',
'app.albums',
'app.users_albums'
];
public function testAdd(){
// $data = new FormData();
$accessToken ='eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjksImV4cCI6MTQ1NzYyOTU3NH0.NnjXWEQCno3PUiwHhnUCBjiknR-NlmT42oPLA5KhuYo';
$http = new Client([
'headers' => ['Authorization' => 'Bearer ' . $accessToken, 'Content-Type' => 'application/json']
]);
$data = [
"album_id" => 1,
"link" => "http://www.google.com",
"description" => "testtesttest",
"favorite" => true
];
$result = $http->post('http://vecto.app/api/pictures/add.json', $data, ['type'=>'json']);
// $this->assertResponseOk();
// debug($result);
}
}
When I try to debug the result I get a 'cannot add or update child row' while I'm sure the responding id does exists
(the fixtures does have the id's too). Additionally, the log indicates that it only tries to insert the create/update rows. Therefore, I'm pretty sure the data is ignored but however I can't find a solution. I already tried different combination of headers like only application/json for Accept, application/json for Content-Type etc. I'm using the CRUD plugin for Cakephp to pass the data to an add function.
Postman output
Furthermore, I tried the Postman Chrome plugin to save the data and that actually does work. Does anyone know what I'm doing wrong in the test?
That's not how the integration test case is ment to be used. You are dispatching an external, real request, which will leave the test environment, while you should use the request dispatching tools that the integration test case supplies, that is
IntegrationTestCase::get()
IntegrationTestCase::post()
IntegrationTestCase::put()
etc...
These methods will dispatch simulated requests that do not leave the test environment, which is crucial for things to work properly, as you want to use test connections, inspect possible exceptions, have access to the used session, etc...
ie, you should do something along the lines of
$accessToken = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjksImV4cCI6MTQ1NzYyOTU3NH0.NnjXWEQCno3PUiwHhnUCBjiknR-NlmT42oPLA5KhuYo';
$this->configRequest([
'headers' => [
'Authorization' => 'Bearer ' . $accessToken,
'Content-Type' => 'application/json'
]
]);
$data = [
"album_id" => 1,
"link" => "http://www.google.com",
"description" => "testtesttest",
"favorite" => true
];
$this->post('/api/pictures/add.json', json_encode($data));
Note that a content type of application/json will require you to send raw JSON data! If you don't actually need/want to test parsing of raw input, then you could skip that header, and pass the array as data instead.
See also
Cookbook > Testing > Controller Integration Testing
API > \Cake\TestSuite\IntegrationTestCase

cURL / wp_remote_post: won't work in wordpress plugin

I'm working on a wordpress plugin, which pull data from another site via a cURL post call. I've tested the same code in i wordpress plugin and outsite of wordpress.
Outsite of wordpress the script works fine, but inside a wordpress plugin, the script just wont work:
Wordpress plugin:
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_POST, true );
curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true );
curl_setopt($handle, CURLOPT_POSTFIELDS, array('postfield' => 'postfieldcontent'));
$result = curl_exec($handle);
Main site:
mysql_query("INSERT INTO table (q, w, e, r, t, y, u) VALUES ('', '".$_POST[postfield]."', '', '', '', '', '')");
I've removed all database security for debugging purpose. I have also tried the wp_remote_post function, that doesn't work either. I've even triede the the wp_remote_get function, but i can access the get variable:
$result = wp_remote_get( 'http://qwerty.dk/folder/filename.php?getfield=qwertrert' );
I've given up - please help :)
Best regards
Kim
You will need to enable cURL in your php.ini file. See #1347146
wp_remote_post() uses a class called WP_Http that in turn can use one of three transport classes (see file class-http.php function _get_first_available_transport).
POST method will work with class WP_Http_Curl, but will not work with class WP_Http_Streams (the cURL fallback).
The alternative is to use wp_remote_get()

Resources