Wordpress : Call wp_create_user function from external? - wordpress

In Wordpress, i want to create New Users from external. I've found this is function in wordpress:
wp_create_user( $username, $password, $email );
So how can i run this function from external call please?
I mean, how to run this function from either:
Via simple URL with GET, like: www.example.com/adduser/?username=james&password=simpletext&email=myemail
Via cURL with POST
.. from external website.

You may try this but also make sure that the listening url has a handler to handle the request in your WordPress end (using GET methof)
function curlAdduser($strUrl)
{
if( empty($strUrl) )
{
return 'Error: invalid Url given';
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $strUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
$return = curl_exec($ch);
curl_close($ch);
return $return;
}
Call the function from external site :
curlAdduser("www.example.com/adduser?username=james&password=simpletext&email=myemail");
Update : using POST method
function curlAdduser($strUrl, $data) {
$fields = '';
foreach($data as $key => $value) {
$fields .= $key . '=' . $value . '&';
}
$fields = rtrim($fields, '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $strUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$return = curl_exec($ch);
curl_close($ch);
return $return;
}
Call the function with data
$data = array(
"username" => "james",
"password" => "simpletext",
"email" => "myemail"
);
curlAdduser("www.example.com/adduser", $data);

Related

I want to add contact form 7 data to zoho crm with custom code in wordpress. I tried but not find any solution

I already added the code on my function.php file by applying zoho crm api but the data is not transferring on ZOHO crm.
I called the data through the access token provided by Zoho crm, then i create API v2 of web API and generate client id and client secret. I don't know where i am making error
Please check and update me where i am making mistake
add_action('wpcf7_mail_sent','brainium_cf7_api_sender');
function brainium_cf7_api_sender(){
$title = $contact_form->title;
if( $title === 'Contact form 1') {
$submission = WPCF7_Submission::get_instance();
if( $submission ){
$posted_data = $submission->get_posted_data();
$first_name = $posted_data['first-name'];
$last_name = $posted_data['last-name'];
$email = $posted_data['your-email'];
$phone = $posted_data['Phone-no'];
$message = $posted_data['your-message'];
$budget = $posted_data['budget'];
$checkbox = $posted_data['checkbox-993'];
$auth = 'xxxxxxxxxxxx';
$refreshToken = "xxxxxxxxxxxx";
//get the last date and time of refresh token generation
//get the access token
$url = "https://accounts.zoho.com/oauth/v2/token";
$query = "refresh_token=xxxxxxxxxxxx&client_id=xxxxxxxxxxxx&client_secret=xxxxxxxxxxxx&grant_type=refresh_token";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
$result = curl_exec($ch);
curl_close($ch);
//get the token from the JSON in result
$accessToken = json_decode($result, true);
//echo ($accessToken['access_token']);
//die();
$data = array("First_Name"=>$first_name, "Last_Name"=>$last_name, "Email"=>$email, "Phone"=>$phone, "Description"=>($message), "Budget" => $budget, 'Subscribed_Newsletter' => $checkbox, "Lead_Date"=>$zoho_date, '$gclid'=>$zc_gad);
//$data = json_encode($data);
$encodedData = array();
$encodedData['data'][0] = $data;
//var_dump($data);
//echo(json_encode($encodedData));
//die();
//che
$url ="https://www.zohoapis.com/crm/v2/Leads";
$headers = array(
'Authorization: Zoho-oauthtoken '.$accessToken['access_token'],
'Content-Type:application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($encodedData));
$result = curl_exec($ch);
curl_close($ch);
//echo $result;
/**** End zoho CRM ****/
$success = true;
$msg = 'Done';
$leadSaving = print_r($result, true);
$leadSaving = $leadSaving . "\r\n" . $email;
$leadSaving = $leadSaving . "\r\n" . "request quote";
file_put_contents("lead-zoho-entry-status.txt", $leadSaving, FILE_APPEND);
file_put_contents("lead-zoho-entry-status.txt", "\r\n\r\n", FILE_APPEND); }
}
}
I tried and getting the following SUCCESS response
{"data":[{"code":"SUCCESS","details":{"Modified_Time":"2022-07-27T17:28:36+05:30","Modified_By":{"name":"Sourav Sinha","id":"1153488000000068001"},"Created_Time":"2022-07-27T17:28:36+05:30","id":"1153488000046560019","Created_By":{"name":"Sourav Sinha","id":"1153488000000068001"}},"message":"record added","status":"success"}]}
youremail#test.com
request quote
This is the code used in functions.php file
add_action('wpcf7_mail_sent','brainium_cf7_api_sender');
function brainium_cf7_api_sender($contact_form){
$title = $contact_form->title;
if( $title === 'Contact form 1') {
$submission = WPCF7_Submission::get_instance();
if( $submission ){
$posted_data = $submission->get_posted_data();
$first_name = $posted_data['first-name'];
$last_name = $posted_data['last-name'];
$email = $posted_data['your-email'];
$phone = $posted_data['Phone-no'];
$message = $posted_data['your-message'];
$budget = $posted_data['budget'];
$checkbox = $posted_data['checkbox-194'][0];
$auth = 'xxxxxxxxxxxx';
$refreshToken = "xxxxxxxxxxxx";
//get the last date and time of refresh token generation
//get the access token
$url = "https://accounts.zoho.com/oauth/v2/token";
$query = "refresh_token=xxxxxxxxxxxx&client_id=xxxxxxxxxxxx&client_secret=xxxxxxxxxxxx&grant_type=refresh_token";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
$result = curl_exec($ch);
curl_close($ch);
//get the token from the JSON in result
$accessToken = json_decode($result, true);
//echo ($accessToken['access_token']);
//die();
$data = array("First_Name"=>$first_name, "Last_Name"=>$last_name, "Email"=>$email, "Phone"=>$phone, "Description"=>($message), "Budget" => $budget, 'Subscribed_Newsletter' => $checkbox, "Lead_Date"=>$zoho_date, '$gclid'=>$zc_gad);
//$data = json_encode($data);
$encodedData = array();
$encodedData['data'][0] = $data;
//var_dump($data);
//echo(json_encode($encodedData));
//die();
//che
$url ="https://www.zohoapis.com/crm/v2/Leads";
$headers = array(
'Authorization: Zoho-oauthtoken '.$accessToken['access_token'],
'Content-Type:application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($encodedData));
$result = curl_exec($ch);
curl_close($ch);
//echo $result;
// End zoho CRM
$success = true;
$msg = 'Done';
$leadSaving = print_r($result, true);
$leadSaving = $leadSaving . "\r\n" . $email;
$leadSaving = $leadSaving . "\r\n" . "request quote";
file_put_contents("lead-zoho-entry-status.txt", $leadSaving, FILE_APPEND);
file_put_contents("lead-zoho-entry-status.txt", "\r\n\r\n", FILE_APPEND);
}
}
}
<?php
$first_name = 'first-name';
$last_name = 'last-name';
$email = 'youremail#test.com';
$phone = '9876543210';
$message = 'your-message';
$budget = '1000';
$checkbox = '1';
$auth = 'fa738eaef1becee890f8935f65169e99';
$refreshToken = "1000.3c0d5bb96a00e9438d132d94a316a72b.2d5261b7e33c42614c08f1bd4e9a0f1a";
//get the last date and time of refresh token generation
//get the access token
$url = "https://accounts.zoho.com/oauth/v2/token";
$query = "refresh_token=xxxxxxxxxxxx&client_id=xxxxxxxxxxxx&client_secret=xxxxxxxxxxxx&grant_type=refresh_token";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
$result = curl_exec($ch);
curl_close($ch);
//get the token from the JSON in result
$accessToken = json_decode($result, true);
//echo ($accessToken['access_token']);
//die();
$data = array("First_Name"=>$first_name, "Last_Name"=>$last_name, "Email"=>$email, "Phone"=>$phone, "Description"=>($message), "Budget" => $budget, 'Subscribed_Newsletter' => $checkbox, "Lead_Date"=>$zoho_date, '$gclid'=>$zc_gad);
//$data = json_encode($data);
$encodedData = array();
$encodedData['data'][0] = $data;
//var_dump($data);
//echo(json_encode($encodedData));
//die();
//che
$url ="https://www.zohoapis.com/crm/v2/Leads";
$headers = array(
'Authorization: Zoho-oauthtoken '.$accessToken['access_token'],
'Content-Type:application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($encodedData));
$result = curl_exec($ch);
curl_close($ch);
//echo $result;
/**** End zoho CRM ****/
$success = true;
$msg = 'Done';
$leadSaving = print_r($result, true);
$leadSaving = $leadSaving . "\r\n" . $email;
$leadSaving = $leadSaving . "\r\n" . "request quote";
file_put_contents("lead-zoho-entry-status.txt", $leadSaving, FILE_APPEND);
file_put_contents("lead-zoho-entry-status.txt", "\r\n\r\n", FILE_APPEND);

I am trying to get contact form 7 data to zoho crm ,it is working fine on local host but on live it is giving following error

It is working fine in localhost but when i updating it on live site it giving following error-
{"data":[{"code":"INVALID_DATA","details":{"maximum_length":20,"api_name":"Subscribed_Newsletter"},"message":"invalid data","status":"error"}]}
You can find the reference code below-
add_action('wpcf7_mail_sent','brainium_cf7_api_sender');
function brainium_cf7_api_sender($contact_form){
$title = $contact_form->title;
if( $title === 'Test') {
$submission = WPCF7_Submission::get_instance();
if( $submission ){
$posted_data = $submission->get_posted_data();
$first_name = $posted_data['first-name'];
$last_name = $posted_data['last-name'];
$email = $posted_data['your-email'];
$phone = $posted_data['Phone-no'];
$message = $posted_data['your-message'];
$budget = $posted_data['budget'];
$checkbox = $posted_data['checkbox-37'][0];
$auth = 'xxxxxxxxxxxx';
$refreshToken = "xxxxxxxxxxxx";
//get the last date and time of refresh token generation
//get the access token
$url = "https://accounts.zoho.com/oauth/v2/token";
$query = "refresh_token=xxxxxxxxxxxx&client_id=xxxxxxxxxxxx&client_secret=xxxxxxxxxxxx&grant_type=refresh_token";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
$result = curl_exec($ch);
curl_close($ch);
//get the token from the JSON in result
$accessToken = json_decode($result, true);
//echo ($accessToken['access_token']);
//die();
$data = array("First_Name"=>$first_name, "Last_Name"=>$last_name, "Email"=>$email, "Phone"=>$phone, "Description"=>($message), "Budget" => $budget, 'Subscribed_Newsletter' => $checkbox, "Lead_Date"=>$zoho_date, '$gclid'=>$zc_gad);
//$data = json_encode($data);
$encodedData = array();
$encodedData['data'][0] = $data;
//var_dump($data);
//echo(json_encode($encodedData));
//die();
//che
$url ="https://www.zohoapis.com/crm/v2/Leads";
$headers = array(
'Authorization: Zoho-oauthtoken '.$accessToken['access_token'],
'Content-Type:application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($encodedData));
$result = curl_exec($ch);
curl_close($ch);
//echo $result;
// End zoho CRM
$success = true;
$msg = 'Done';
$leadSaving = print_r($result, true);
$leadSaving = $leadSaving . "\r\n" . $email;
$leadSaving = $leadSaving . "\r\n" . "request quote";
file_put_contents("lead-zoho-entry-status.txt", $leadSaving, FILE_APPEND);
file_put_contents("lead-zoho-entry-status.txt", "\r\n\r\n", FILE_APPEND);
}
}
}

send push notification to android using Laravel

I want to send notification to android and IOS devices using laravel 5.8. Please help me on recommending the best package for sending push notification.
when using this funcation This error appears "{"multicast_id":6507423456439771247,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}"
public function sendPushNotification($fcm_token, $title, $message, $id = null) {
$url = "https://fcm.googleapis.com/fcm/send";
$header = [
'authorization:key=key',
'content-type: application/json'
];
$postdata = '{
"to" : "' . $fcm_token . '",
"notification" : {
"title":"' . $title . '",
"text" : "' . $message . '"
},
"data" : {
"id" : "'.$id.'",
"title":"' . $title . '",
"description" : "' . $message . '",
"text" : "' . $message . '",
"is_read": 0
}
}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}

Send wordpress form data to odoo for leads via post request

I'm new to wordpress and would like to generate leads on odoo crm. I would like to send the form data in contact us form to odoo via a post request. I'm currently using wpforms plugin.
<?php
add_action( 'wpcf7_mail_sent', 'action__wpcf7_mail_sent' );
function action__wpcf7_mail_sent( $contact_form ) {
$title = $contact_form->title();
$submission = WPCF7_Submission::get_instance();
if ( $submission ) {
$posted_data = $submission->get_posted_data();
}
function post_to_url( $url, $data ) {
$fields = '';
foreach( $data as $key => $value ) {
$fields .= $key . '=' . $value . '&';
}
rtrim( $fields, '&' );
$timeout = 30;
$post = curl_init();
curl_setopt($post, CURLOPT_URL, $url);
curl_setopt($post, CURLOPT_HEADER, 0);
curl_setopt ($post, CURLOPT_TIMEOUT, $timeout);
curl_setopt ($post, CURLOPT_USERAGENT, sprintf("Mozilla/%d.0",rand(4,5)));
curl_setopt ($post, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($post, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($post, CURLOPT_POST, true);
curl_setopt($post, CURLOPT_POSTFIELDS, ($fields));
curl_setopt($post, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($post, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($post);
curl_close($post);
}
if ( 'your contact form title' == $title ) {
//you will get data in $posted_data variable
//echo "<pre>"; print_r($posted_data);
$data = $posted_data;
$final_url = "your_api_url";
post_to_url( $final_url, $data );
}
}
try using this hook wpcf7_mail_sent

Woocommerce Checkout function

I want to execute below script while doing checkout in woocommerce.
$args = http_build_query(array(
'token' => '*********',
'from' => '****',
'to' => '*******',
'text' => 'Your Order Id is: 9879987 '));
$url = "http://api.sparrowsms.com/v2/sms/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$args);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
I didn't find checkout process page.
You need to add a woocommerce_after_checkout_form hook in function.php like below.
add_action( 'woocommerce_after_checkout_form', 'sms_function', 10 );
function sms_function( ) {
$args = http_build_query(array(
'token' => '*********',
'from' => '****',
'to' => '*******',
'text' => 'Your Order Id is: 9879987 '));
$url = "http://api.sparrowsms.com/v2/sms/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$args);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
}

Resources