Mailchimp PHP Marketing API pass request parameters - mailchimp

I am using Mailchimp's Marketing PHP API (https://github.com/mailchimp/mailchimp-marketing-php) to retrieve the templates list. It has the following code.
<?php
require_once '/path/to/MailchimpMarketing/vendor/autoload.php';
$client = new MailchimpMarketing\ApiClient();
$client->setConfig([
'apiKey' => 'YOUR_API_KEY',
'server' => 'YOUR_SERVER_PREFIX',
]);
$response = $client->templates->list();
print_r($response);
And it returns the whole set of other default templates (128 templates!), the api request parameter has type field which can be used to filter these templates. But I could not find anyway to pass the request parameter. Any idea?

I am not sure if this is a correct approach but after going through API library source code, I found the correct function to include request parameters.
<?php
require_once '/path/to/MailchimpMarketing/vendor/autoload.php';
$client = new MailchimpMarketing\ApiClient();
$client->setConfig([
'apiKey' => 'YOUR_API_KEY',
'server' => 'YOUR_SERVER_PREFIX',
]);
$response = $this->client->templates->listWithHttpInfo($fields = null, $exclude_fields = null, $count = '10', $offset = '0', $created_by = null, $since_created_at = null, $before_created_at = null, $type = 'user', $category = null, $folder_id = null, $sort_field = null);
print_r($response);

Related

How to post request with query params in laravel 9

I am trying to send post request with query params. just like this screenshot
I used Http::post but it supports only json as far as I know. I just want to send request just like the screenshot. cause API data only supports parameter wise. Here What I tried but failed to achieve that.
FormController.php:
public function post_parameter_wise(Request $request,$lp_campaign_id, $lp_campaign_key, $first_name, $last_name, $email, $phone, $zip_code){
$form = new Form();
$fName = $form->first_name = $request->get($first_name);
$lName = $form->last_name = $request->get($last_name);
$cCmail = $form->email = $request->get($email);
$cPhone = $form->phone = $request->get($phone);
$zCode = $form->zip_code = $request->get($zip_code);
$response = Http::post("https://t.vivint.com/post.do", [
"lp_campaign_id" => $lp_campaign_id,
"lp_campaign_key" => $lp_campaign_key,
// "lp_supplier_id" => "",
"first_name" => $fName,
"last_name" => $lName,
"email" => $cCmail,
"phone" => $cPhone,
"zip_code" => $zCode
]);
dd($response);
Can't leave a comment so disregard this as the answer
Preferably, you just need to change it from a Post request to a Get request and it will work
$response = Http::get("https://t.vivint.com/post.do", [...
But a post request will contain the data as a post request should.
If you have to use post and define the query params then you should do it as follows
public function post_parameter_wise(Request $request,$lp_campaign_id, $lp_campaign_key, $first_name, $last_name, $email, $phone, $zip_code){
$form = new Form();
$fName = $form->first_name = $request->get($first_name);
$lName = $form->last_name = $request->get($last_name);
$cCmail = $form->email = $request->get($email);
$cPhone = $form->phone = $request->get($phone);
$zCode = $form->zip_code = $request->get($zip_code);
$response = Http::post("https://t.vivint.com/post.do?lp_campaign_id=".$lp_campaign_id."&lp_campaign_key=".$lp_campaign_key."&first_name=".$fName."&last_name=".$lName."&email=".$cCmail."&phone=".$cPhone."&zip_code=".$zCode, []);
dd($response);
Although this is really just forcing it to

file_get_contents() returning false for Google reCAPTCHA v3

I have been trying to implement Google reCAPTCHA v3 by using the following PHP source code:
<script src="https://www.google.com/recaptcha/api.js?render=6Ldl-98fAAAAAKRPodblUlTcVgvrfWZ_8lODjmZA"></script>
<?php
if(isset($_POST) && isset($_POST["btnSubmit"]))
{
$secretKey = '6Ldl-98fAAAAAD3ekajHHVBi2X4fZTW37bI5IGUN';
$token = $_POST["g-token"];
$ip = $_SERVER['REMOTE_ADDR'];
$url = "https://www.google.com/recaptcha/api/siteverify";
$data = array('secret' => $secretKey, 'response' => $token, 'remoteip'=> $ip);
// use key 'http' even if you send the request to https://...
$options = array('http' => array(
'method' => 'POST',
'content' => http_build_query($data)
));
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
$response = json_decode($result);
if($response->success)
{
echo '<center><h1>Validation Success!</h1></center>';
}
else
{
echo '<center><h1>Captcha Validation Failed..!</h1></center>';
}
}
?>
The problem is that every time I run this and step through the code line by line, I notice that the file_get_contents() function is always returning false and is assigning false to the '$result' variable. Why is this?
$result = file_get_contents($url, false, $context);
I would appreciate any advice that I can get.
From docs: 'null is returned if the json cannot be decoded or if the encoded data is deeper than the nesting limit.'
Show us $response, so var_dump($response). In recaptcha v2 i had empty string or token lately. That's why I ask for response, json decode will decode if there is proper json value.
I'd bet you get empty result and json_decode fails.

How can I import users in contact list through hook or API in sugarcrm?

I want to integrate users of my Joomla site into sugarcrm panel. I'm using SuiteCRM Version 7.7.6 [Sugar Version 6.5.24 (Build 509)] and trying to import csv file of users into contact list of sugarcrm through hook.
Means I want to build functionality for auto integration in between Joomla and Sugarcrm site. When a new user register on Joomla site, they should be auto added in the contact list of crm panel.
Is there any possible way to implement this integration ?
I have got a solution to integrate records in a particular module of sugarcrm through Rest API.
Create php file anywhere outside your CRM project directory and write API code as given below.
Need to set site URL of sugarcrm and configure admin username and password in API.
After that, set module name in which you want to add data.
Adjust your data in given array format.
Now just hit the file URL on browser where you placed this API.
Data will be successfully added in the required module.
API Code:
$url = "http://{site_url}/service/v4_1/rest.php";
$username = "admin";
$password = "password";
//function to make cURL request
function call($method, $parameters, $url)
{
ob_start();
$curl_reque**strong text**st = curl_init();
curl_setopt($curl_request, CURLOPT_URL, $url);
curl_setopt($curl_request, CURLOPT_POST, 1);
curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($curl_request, CURLOPT_HEADER, 1);
curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0);
$jsonEncodedData = json_encode($parameters);
$post = array(
"method" => $method,
"input_type" => "JSON",
"response_type" => "JSON",
"rest_data" => $jsonEncodedData
);
curl_setopt($curl_request, CURLOPT_POSTFIELDS, $post);
$result = curl_exec($curl_request);
curl_close($curl_request);
$result = explode("\r\n\r\n", $result, 2);
$response = json_decode($result[1]);
ob_end_flush();
return $response;
}
//login --------------------------------------------
$login_parameters = array(
"user_auth" => array(
"user_name" => $username,
"password" => md5($password),
"version" => "1"
),
"application_name" => "RestTest",
"name_value_list" => array(),
);
$login_result = call("login", $login_parameters, $url);
/*
echo "<pre>";
print_r($login_result);
echo "</pre>";
*/
//get session id
$session_id = $login_result->id;
//create contacts ------------------------------------
$set_entries_parameters = array(
//session id
"session" => $session_id,
//The name of the module from which to retrieve records.
"module_name" => "Contacts",
//Record attributes
"name_value_list" => array(
array(
//to update a record, you will nee to pass in a record id as commented below
//array("name" => "id", "value" => "912e58c0-73e9-9cb6-c84e-4ff34d62620e"),
array("name" => "first_name", "value" => "John"),
array("name" => "last_name", "value" => "Smith"),
),
array(
//to update a record, you will nee to pass in a record id as commented below
//array("name" => "id", "value" => "99d6ddfd-7d52-d45b-eba8-4ff34d684964"),
array("name" => "first_name", "value" => "Jane"),
array("name" => "last_name", "value" => "Doe"),
),
),
);
$set_entries_result = call("set_entries", $set_entries_parameters, $url);
echo "<pre>";
print_r($set_entries_result);
echo "</pre>";
Click here for more details:
http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_6.5/Application_Framework/Web_Services/Examples/REST/PHP/Creating_or_Updating_Multiple_Records/
Yes it would be possible, although not practicable to do in a hook.
If you need to import a CSV more than once, implementing a scheduler job for the import might be a good idea.
However you could actually not use CSV at all and let Joomla or an intermediate script push the data to sugar in real-time or a time interval of your choice using the REST API of your CRM.

Redirect in Slim

I have an ajax request, but I need my response to redirect to a certain twig template. This is what I have:
$response = ['success'=>false];
return $this->view->render($resp, 'spotlight-results.twig',['data' => $response] );
The problem with this is I am receiving the page in the console, this is not replacing the page that makes the request. I don't know if I explained myself correctly...
I have found the answer:
$loader = new Twig_Loader_Filesystem('../templates/');
$twig = new Twig_Environment($loader);
$template = $twig->loadTemplate('spotlight-results.twig');
$message = ['success' => false];
$view = $template->render(['data' => $message]);
$response = ['success' => false, 'view' => $view];
This way I return my template, with my desired content, $message

How to add limit option in Magento API call

I am creating web service for my store. I am using magento API to collect product list from store. But it display all the 500 records. And i want it 25 records per page. What to add in API call? Or What filter will be apply for this?
//create soap object
$proxy = new SoapClient('http://localhsot/magento/api/soap/?wsdl');
// create authorized session id using api user name and api key
// $sessionId = $proxy->login('apiUser', 'apiKey');
$sessionId = $proxy->login('test_admin', '12345678');
$filters = array(
);
// Get list of product
$productlist = $proxy->call($sessionId, 'product.list', array($filters));
print_r($productlist );
that didn't work, can you limit it by id like:
$filters = array('id'=>array("gteq"=>1), 'id'=>array("lteq"=>1000));
that way you could add some paging?
I know this is an old question but I struggled with this. I've created my own SOAP API endpoint which does exactly the same as the default catalogProductList function but has an extra param. See the code below:
$collection = Mage::getModel('catalog/product')->getCollection()
->addStoreFilter($this->_getStoreId($store))
->addAttributeToSelect('name');
if($limit) {
$collection->setOrder('updated_at', 'ASC');
$collection->setPageSize($limit);
}
/** #var $apiHelper Mage_Api_Helper_Data */
$apiHelper = Mage::helper('api');
$filters = $apiHelper->parseFilters($filters, $this->_filtersMap);
try {
foreach ($filters as $field => $value) {
$collection->addFieldToFilter($field, $value);
}
} catch (Mage_Core_Exception $e) {
$this->_fault('filters_invalid', $e->getMessage());
}
$result = array();
foreach ($collection as $product) {
$result[] = array(
'product_id' => $product->getId(),
'sku' => $product->getSku(),
'name' => $product->getName(),
'set' => $product->getAttributeSetId(),
'type' => $product->getTypeId(),
'category_ids' => $product->getCategoryIds(),
'website_ids' => $product->getWebsiteIds(),
'updated_at' => $product->getUpdatedAt(),
'created_at' => $product->getCreatedAt()
);
}
return $result;
And from there we keep track of the last updated_at value and use that as a filter to get the next [LIMIT] items. By default updated_at and created_at are not in the response and the list is not orderred by updated_at so I've added this.
I am not sure of this but you could try this, as the API is working with collections and this is the way to limit a collection size
$filters = array(
'setPageSize' => 10
);

Resources