How to update a Deal custom field in Pipedrive API? - pipedrive-api

I have a list of custom fields for a Deals in Pipedrive. (Located here https://your-instance.pipedrive.com/settings/fields?type=DEAL)
Each custom field has a custom API Key which looks like this a56aff894af47285d3bbcd67fe06ee5143fb1123
I try to update these fields in a Deal in bulk.
According to the Update a deal documentation https://developers.pipedrive.com/docs/api/v1/#!/Deals/put_deals_id there is no information about these custom fields.
The question is how to update custom field value in a certain Deal?

To achieve this you can send custom field key - value pairs just in the request body.
According to this example:
// Pipedrive API token
$api_token = '659c9fddb16335e48cc67114694b52074e812';
// Custom field key value pairs
$data = array(
'dcf558aac1ae4e8c4f849ba5e668430d8df9be12' => 'Custom field value'
);
// Deal ID
$deal_id = 260;
// URL for updating a Deal
$url = 'https://your-instance.pipedrive.com/api/v1/deals/' . $deal_id . '?api_token=' . $api_token;
// Prepare CURL Request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$output = curl_exec($ch);
curl_close($ch);

Related

how do i send notification to all mobile devices when i post a new content in Drupal? also Laravel is my Authentication back-end

I am using Laravel for authentication and druapl for content management, and now using firebase for notification to mobile devices... how do i send notification to all mobile devices when i post a new content in druapl?
this is my send function in Laravel
public function sendNotification($device_token, $message)
{
$SERVER_API_KEY = 'myKey';
$data = [
"to" => $device_token, // for single device id
"notification" => $message
];
$dataString = json_encode($data);
$headers = [
'Authorization: key=' . $SERVER_API_KEY,
'Content-Type: application/json',
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
You should get token id from each user device and send notification via their device token as an array.
Add a new table named devices has user_id and device_token as columns then each new user open your application his device token will be sent to your server then you should save received token as new record to devices table.

curl with laravel return session expired

I use curl with laravel and I need to launch a function inside a controller.
but when I put
dd('test');
after the namespace it's print.but when I try with the function a have a session expired
this is the code for the curl
// Préparation des données
$data = [];
$data["code__client"] = "VERTDIS13";
$data["status"] = "90";
$data["numero__incident"] = "INC544842";
// On tranforme le tableau PHP en objet JSON
$data = json_encode($data);
// Envoi au serveur
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://localhost:3000/notification/server");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "data=" . $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, true);
$server_output = curl_exec($ch);
$error = curl_error($ch);
if($error!=""){ dd($error);}
curl_close($ch);
// Ici pour tester j'affiche ce que le serveur renvoi
return $server_output;
inside the controller I tried to print data but it doesn't work
do you see the issue ?
thanks for help .
The session expired response sounds to me as if you are performing multiple consecutive curl requests to your application. If that's the case and your application depends on session cookies you can create a simple text file that is read and writeable to the script that does the curl requests, and add this to your code:
$cookie_file = "/the/path/to/your/cookiefile.txt";
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
Change the path in $cookie_file to the full filesystem path of the text file you create and curl will persist session cookies that are being returned with responses and use them in consecutive requests.
For this to work I think you should remove the line
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
because this would advise curl to ignore the cookies in the file.
Besides that I think the line
curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, true);
is also obsolete, because you don't provide basic auth credentials in your curl request.
I presume you extract data from within the application?
To me, it looks like CSRF token is missing or is in fact invalid.
for ($i = 0; $i < $metas->length; $i++){
$meta = $metas->item($i);
if($meta->getAttribute('name') == 'csrf-token')
$csrfToken = $meta->getAttribute('content');
}
$headers = array();
$headers[] = "Cookie: X-CSRF-Token=$csrfToken";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
More about CSRF & cURL
Give this a go

Quick q. Mailchimp API 3.0

Can i use the first script or i need to use the curl option for mailchimp 3.0? I read some posts that the first one may be depreciated.. is that correct? Note that i'm not running on WordPress. Thank you for your fast answers.
<?php
require("vendor/autoload.php");
use \DrewM\MailChimp\MailChimp;
$mc = new MailChimp('apikey');
$email = $_POST['email'];
$subscriber_hash = $mc->subscriberHash($email);
$response = [];
$list_id = 'listid';
$resp = $mc->get("/lists/$list_id/members/$subscriber_hash";
if ($mc->success()) {
$response['message'] = 'Thank you for subscribing to the mailing list';
// User successfully subscribed - set HTTP status code to 200
http_response_code(200);
} else {
$response['message'] = $mc->getLastError();
// User not subscribed - set HTTP status code to 400
http_response_code(400);
}
// Return json-formatted response
echo json_encode($response);
?>
Or should i use this one?
function mc_checklist($email, $debug, $apikey, $listid, $server) {
$userid = md5($email);
$auth = base64_encode( 'user:'. $apikey );
$data = array(
'apikey' => $apikey,
'email_address' => $email
);
$json_data = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://'.$server.'.api.mailchimp.com/3.0/lists/'.$listid.'/members/' . $userid);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
'Authorization: Basic '. $auth));
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
$result = curl_exec($ch);
if ($debug) {
var_dump($result);
}
$json = json_decode($result);
echo $json->{'status'};
}
This row is only for stackoverflow not alowing me to post that much code without including more details.
The first is not deprecated. It uses this wrapper to make API calls using the same API version that the second block of code uses. It's just simpler to work with so you don't have to write separate CURL requests for every call. You can also take a look at some of its source code and notice that it uses CURL anyway to make its calls.
So either way will use CURL, and whichever option you choose is simply a matter of preference.
Hopefully that clears it up for you!

Mailchimp API status 200 for members who are already subscribed

The following code is working and subscribing new members successfully. However, the status returns 200 even if the email address is already subscribed. I have checked that $member_id is hashing correctly, and despite identical values it still returns 200.
$result = array(
'status' => sync_mailchimp($data)
);
var_dump($result) // 200
function sync_mailchimp($data) {
// Setup our Mailchimp info
$api_key = 'xxxxx';
$list_id = 'ab8abde5bb';
$member_id = md5(strtolower($data['email'])); // lowercase hash of the email
$datacenter = 'us16';
$url = 'https://' . $datacenter . '.api.mailchimp.com/3.0/lists/' . $list_id . '/members/' . $member_id;
$json = json_encode([
'email_address' => $data['email'],
'status' => $data['status']
]);
// Send via curl
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $api_key);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
$result = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $httpcode;
}
I was also having the same issue while using a similar snippet.
Reading the MailChimp API 3.0 docs relating to managing subscribers (specifically the section Subscribe an Address), it was clear that this method was not the correct way to add a new subscriber.
The request type needs to be a POST
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
and must be sent to the endpoint
https://<data_center>.api.mailchimp.com/3.0/lists/<list_id>/members/
The request body containing the JSON is identical.
The response code will be 200 when an email address is added to the list successfully, and 400 when the email address already exists on the list.
Note that this method will not work for updating an existing subscriber – to do that, the request type must be PATCH, and you must use the original endpoint ending with the hashed email address.

How can I update custom fields using the BigCommerce API?

I am looking to update the custom fields on a product.
I found in the guide on how to do it manually via the admin interface
The API docs suggest that you can't directly modify the custom fields on a product, only access them:
My next thought was to update the product
This is the existing custom_field on the product.
{
"url"=> "https://storename.mybigcommerce.com/api/v2/products/32/customfields.json",
"resource"=>"/products/32/customfields"
}
When I try to modify the url/resource and send the hash back to update, I am greeted with a 400 Bad Request :(
new_custom_fields = {
"url" => "https://storename.mybigcommerce.com/api/v2/products/75/customfields.json",
"resource" => "/products/75/customfields"
}
api.update_products(75, {"custom_fields" => new_custom_fields})
RuntimeError: Failed to parse Bigcommerce response: 400 Bad Request
Thoughts?
this seems to be a bug in the Bigcommerce APIs. Currently only GET requests on the custom fields are supported.
http://developer.bigcommerce.com/api/products/customfields
That is probably the reason why you are hitting a 400.
Not sure if this will help with Ruby, but it might help those using PHP... I'm able to create a custom field on a product using php. Just need the product id and the values for the custom field, "name" and "text".
$data_array = array('name' => 'gender', 'text' => 'male');
BigCommerce::createProductCustomField('17', $data_array);
I haven't tried updating a custom field, but if creating one works, then the following should also work to update a current custom field:
BigCommerce::updateProductCustomField($product_id, $id, $object);
You'll need the $product_id of the product you want to update, the $id of the custom field that you want to update, and $object should be an array like $data_array above.
More info on PHP client for BC: https://github.com/bigcommerce/bigcommerce-api-php
Good luck!
Try this Code :
$headers = array(
"Content-type: application/json",
//"Authorization: Basic " . base64_encode($credentials)
);
$name='Bullet Point ';
$data_array = array('name'=>'Bullet Point','text'=>'Bullet Point value');
$body=json_encode($data_array);
//Get the current url and split it at the '?'
$ch = curl_init('https://www.abc.mybigcommerce.com/api/v2/products/1122/customfields.json'); //open connection
curl_setopt($ch, CURLOPT_TIMEOUT, 60); //set to 60 seconds from BC API Guide v1 PDF example
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); //load all header data
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); //comment out this PUT line to change to a POST statement
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
curl_setopt( $ch, CURLOPT_USERPWD, "admin:api-key");
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
$result = curl_exec($ch); //execute post
curl_close($ch);

Resources