Encoding issues when posting custom fields via wordpress xmlrpc api - wordpress

I've a problem with wordpress xml-rpc api. My code gets some data from an xml and posts to a blog. Page title posted well, there is no problem on blog but custom fields are broken.
Code file, xml, blog settings and database tables they are all utf-8 encoded.
function wpPostXMLRPC($title,$body,$rpcurl,$username,$password,$thumbnail,$cfields,$category,$keywords='',$encoding='UTF-8') {
$title = html_entity_decode(htmlentities($title,ENT_NOQUOTES,$encoding));
$body = html_entity_decode(htmlentities($body,ENT_NOQUOTES,$encoding));
$keywords = html_entity_decode(htmlentities($keywords,ENT_NOQUOTES,$encoding));
array_walk($cfields,arr_encoding); // this function does the same thing with above
$content = array(
'title'=>$title,
'description'=>$body,
'mt_allow_comments'=>0, // 1 to allow comments
'mt_allow_pings'=>0, // 1 to allow trackbacks
'post_type'=>'post',
'mt_keywords'=>$keywords,
'categories'=>array($category),
'custom_fields' => $cfields
);
$params = array(0,$username,$password,$content,true);
$request = xmlrpc_encode_request('metaWeblog.newPost',$params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($ch, CURLOPT_URL, $rpcurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
curl_setopt($ch, CURLOPT_ENCODING, "UTF-8" );
$results = curl_exec($ch);
if(curl_errno($ch))
echo '<hr>curl error:'.curl_error($ch)."<hr>";
curl_close($ch);
return $results;}
and this is arr encoding function:
function arr_encoding($cfields){
if(is_array($cfields))
array_walk($cfields, 'arr_encoding');
else if(is_string($cfields))
$cfields = html_entity_decode(htmlentities($cfields,ENT_NOQUOTES,"UTF-8"));}
Do you have any idea?

OK, here it is:
Don't use
xmlrpc_encode_request('blogger.newPost',$params);
and use:
xmlrpc_encode_request('blogger.newPost',$params,
array('encoding'=>'UTF-8','escaping'=>'markup'));

Related

Filter posts tags by wordpress api

I created a post-type called "material", with taxonomies of type tags called "ano". I'm trying to filter through the wordpress rest api a specific value for that "ano (years)", but it doesn't work, it returns all posts, including those that don't have the specific tag.
<?php
$url = 'https://cat.site.com/wp-json/wp/v2/material?filter[taxonomy]=ano&filter[term]=1994';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response);
var_dump($response);
?>
I found the solution, I used the tag ID:
https://cat.site.com/wp-json/wp/v2/material?ano=28

Contact form 7 loading infinitely after connecting it to our CRM API

Contact form 7 loading infinitely after connecting it to our CRM API though the email is being sent and data getting submitted and saved the user feels that the data has not yet been submitted since the loader keeps moving.
Here's my code added in functions.php
add_action('wpcf7_mail_sent', function ($cf7) {
if(isset($_POST['your-name']) && isset($_POST['your-email']) && isset($_POST['mobile_no'])){
$name = $_POST['your-name'];
$email = $_POST['your-email'];
$phone = $_POST['mobile_no'];
$data = array(
"name"=>"$name",
"email"=>"$email",
"mobile"=>"$phone",
"secret_key"=>"{secret_key}",
);
leadToNoPaperFormAPI($data);
}
});
function leadToNoPaperFormAPI($data=array())
{
if(is_array($data) && !empty($data))
{
$data_string = json_encode($data);
$ch = curl_init('{API_URL}');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
$array = json_decode($result);
}
}
Any help will be highly appreciated. :)
Please note the code gets executed and the form gets submitted & email sent, however, the user on the browser doesn't see the success message just sees the loader.

Integrating SMS api with woocommerce , Not sending messages

I'm integrating SMS API with WooCommerce to send automatic order updates to customers mobiles whenever the make any purchase on site.
below is my code for the same
add_action('woocommerce_payment_complete', 'custom_process_order', 10, 1);
function custom_process_order($billing_phone)
{
$username = "my username";
$hash = "d761fbd7bd31c5eeec2a5b2556d6b9d3b1a1ae51";
//Multiple mobiles numbers separated by comma
$mobileNumber = "$billing_phone";
$senderId = "ORNGMT";
$message = urlencode("Dear Customer");
$postData = array(
'hash' => $hash,
'mobiles' => $$billing_phone,
'message' => $message,
'sender' => $senderId,
);
$url='http://api.textlocal.in/send/?';
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postData
//,CURLOPT_FOLLOWLOCATION => true
));
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$output = curl_exec($ch);
if(curl_errno($ch))
{
echo 'error:' . curl_error($ch);
}
curl_close($ch);
echo $output;
}
is it correct ?
I've added this code to functions.php page
my sms gateway provider has sent me below example code to send sms with PHP
<?php
// Authorisation details.
$username = "your login id";
$hash = "your hash key";
// Configuration variables. Consult http://api.textlocal.in/docs for more info.
$test = "0";
// Data for text message. This is the text message data.
$sender = "API Test"; // This is who the message appears to be from.
$numbers = "44777000000"; // A single number or a comma-seperated list of numbers
$message = "This is a test message from the PHP API script.";
// 612 chars or less
// A single number or a comma-seperated list of numbers
$message = urlencode($message);
$data = "username=".$username."&hash=".$hash."&message=".$message."&sender=".$sender."&numbers=".$numbers."&test=".$test;
$ch = curl_init('http://api.textlocal.in/send/?');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch); // This is the result from the API
curl_close($ch);
?>
Integrating message API for each event will be difficult and you need to customize the at the code level.
You can use a popular SMSplugin for wordpress woocommerce
https://wordpress.org/plugins/woocommerce-apg-sms-notifications/
Just you need to configure this plugin from woocommerce Admin login and it will send sms notifications automatically. We are using it with Spring Edge sms gateway.. Working Good.
Tested and working
Custom api Integration with Woocommerce wordpress
Ufone pakistan sms integration with woocommerce wordpress
if you are looking for integration with ufone pakistan sms api bsms ufone pakistan service provider with woocommerce wordpress then use the following code in your functions file
sms api integration ufone bsms with wordpress woocommerce thanks to the author on this page Integrating custom SMS API with woocommerce
//add this line for calling your function on creation of order in woocommerce
add_action('woocommerce_order_status_processing', 'custom_func', 10, 3);
function custom_func ($order_id) {
$order_details = new WC_Order($order_id);
//fetch all required fields
$billing_phone = $order_details->get_billing_phone();
$billing_name = $order_details->get_billing_first_name();
$billing_last_name = $order_details->get_billing_last_name();
$total_bill = $order_details->get_total();
$textmessage = rawurlencode("Dear $billing_name, Thank you for your order. Your order #$order_id is being processed. Please wait for confirmation call Total Bill = $total_bill");
// Now put HTTP ufone BSMS API URL
$url = "https://bsms.ufone.com/bsms_v8_api/sendapi-0.3.jsp?id=msisdn&message=$textmessage&shortcode=SHORTCODE&lang=English&mobilenum=$billing_phone&password=password&messagetype=Nontransactional";
// NOW WILL CALL FUNCTION CURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
return $order_id;
}
$billingphone in your function is ID. Try this below code. It works
add_action('woocommerce_payment_complete', 'custom_process_order', 10, 1);
function custom_process_order($order_id) {
//Lets get data about the order made
$order = new WC_Order( $order_id );
//Now will fetch customer/buyer id here
$customer_id = $order->user_id;
//now finally we fetch phone number
$billing_phone = get_user_meta( $customer_id, 'billing_phone', true );
// Now put your HTTP SMS API URL . I PUT WHICH WE ARE USING
$jsonurl = "http://tsms.thirdeyegoa.com/api/sendmsg.php?user=USERNAME&pass=PASSWORD&sender=MYSENDERID&phone=".$billing_phone."&priority=ndnd&stype=normal&text=MY MESSAGE TO CUSTOMER.";
// NOW WILL CALL FUNCTION CURL
$json = curl($jsonurl);
return $order_id;
}
Same you can do for Order Completed too with respective hook.
i write this piece of code and it is working.
add_action('woocommerce_order_status_completed','payment_complete');
function payment_complete($order_id)
{
$order=new Wc_order($order_id);
$order_meta = get_post_meta($order_id);
$shipping_first_name = $order_meta['_shipping_first_name'][0];
$phone=$order_meta['_billing_phone'][0];
$first_name=$order->billing_first_name;
$last_name=$order->billing_last_name;
$name=$first_name."".$last_name;
$mobile=$order->billing_mobile;
$items = $order->get_items();
foreach ( $items as $item ) {
$product_name = $item['name'];
$product_id = $item['product_id'];
$product_variation_id = $item['variation_id'];
$time=$item['timings'];
$slot=$item['time_slot'];
$qty= $item['qty'];
$date=$item['tour_date'];
}
$text="your message here";
$data="user=username&pass=password&sender=sendername&phone=".$phone."&priority=ndnd&stype=normal&text=".$text;
$ch = curl_init('http://bhashsms.com/api/sendmsg.php?');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
echo $result;
curl_close($ch);
}
I am from Textlocal. Let me help you out with the code.
Firstly the error that you are getting - 'Unexpected Token' is generally due to an invalid JSON response. If you are unaware of the source of the issue, I would suggest you to look in the Javascript console in the Dev section (triggering the error). For reference, you can check Mike's blog out here.
In the code you have shared, Textlocal API is not receiving the parameters that are mandatory for a successful POST call. You can rephrase your function like this:
add_action('woocommerce_payment_complete', 'custom_process_order', 10, 1);
function custom_process_order($order_id) {
$order = new WC_Order( $order_id );
$customer_id = $order->user_id;
$billing_phone = get_user_meta( $customer_id, 'billing_phone', true );
$data="username=USERNAME&hash=hash&sender=ORNGMT&numbers=".$billing_phone."&message=MY MESSAGE TO CUSTOMER.";
$jsonurl = curl_init('https://api.textlocal.in/send?');
$json = curl($jsonurl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($json);
echo $result;
curl_close($json);
return $order_id;
}
If you need any further assistance, please reach out to our support team (support#textlocal.in)

How to add posts in Wordpress without admin login using xml-rpc and curl

I wants to add posts in Wordpress site using xml-rpc and curl OR any alternate method without logging in WP admin. Is this possible?
I got the answer and Following code is working for me.
function wpPostXMLRPC ($title,$body,$rpcurl,$username,$password,$category='Uncategorized',$keywords='',$encoding='UTF-8') {
$title = htmlentities($title,ENT_NOQUOTES,$encoding);
$keywords = htmlentities($keywords,ENT_NOQUOTES,$encoding);
$content = array (
'title'=>$title,
'description'=>$body,
'mt_allow_comments'=>0,
'mt_allow_pings'=>0,
'post_type'=>'post',
'mt_keywords'=>$keywords,
'categories'=>array(
$category
)
);
$params = array(
0,
$username,
$password,
$content,
true
);
$request = xmlrpc_encode_request('metaWeblog.newPost', $params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($ch, CURLOPT_URL, $rpcurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
$results = curl_exec($ch);
curl_close($ch);
return $results;
}
$title = 'post-title';
$body = 'POST BODY WILL GOES HERE';
$rpcurl = 'http://example.com/xmlrpc.php';
$username = 'xxx';
$password = 'xxx';
$category = 'test';
$chk = wpPostXMLRPC($title,$body,$rpcurl,$username,$password,$category,$keywords='',$encoding='UTF-8');
if($chk) {
echo $chk;
} else {
echo 'failed';
}
There is a built-in feature in Wordpress that allow you to publish an article via e-mail. Never tested it though, but it may suit your needs.
You can configure it in your site's admin, under settings > writing (URL is like http://yourwordpresssite.com/wp-admin/options-writing.php).
However, if you need an API, you'll find useful things in Wordpress Codex pages related to XML-RPC support and wp.newPost.
For a code sample, you can have a look at this snippet, which seems a good basis to me.

Rcurl not working with redirect [duplicate]

I'm using curl to fill a form. After completion of the post the other script which handles the form is redirecting to another URL. I want to get this redirect URL into a variable.
Easy way to find the redirected url (if you don't want to know in advance)
$last_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
You would use
curl_setopt($CURL, CURLOPT_HEADER, TRUE);
And parse the headers for the location header
Here I get the resource http headers then I parse the headers out into an array $retVal. I got the code for parsing the headers from here (http://www.bhootnath.in/blog/2010/10/parse-http-headers-in-php/) You could also use http://php.net/manual/en/function.http-parse-headers.php if you have (PECL pecl_http >= 0.10.0)
$ch = curl_init();
$timeout = 0;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
// Getting binary data
$header = curl_exec($ch);
$retVal = array();
$fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $header));
foreach( $fields as $field ) {
if( preg_match('/([^:]+): (.+)/m', $field, $match) ) {
$match[1] = preg_replace('/(?<=^|[\x09\x20\x2D])./e', 'strtoupper("\0")', strtolower(trim($match[1])));
if( isset($retVal[$match[1]]) ) {
$retVal[$match[1]] = array($retVal[$match[1]], $match[2]);
} else {
$retVal[$match[1]] = trim($match[2]);
}
}
}
//here is the header info parsed out
echo '<pre>';
print_r($retVal);
echo '</pre>';
//here is the redirect
if (isset($retVal['Location'])){
echo $retVal['Location'];
} else {
//keep in mind that if it is a direct link to the image the location header will be missing
echo $_GET[$urlKey];
}
curl_close($ch);
You may want to set the CURLOPT_FOLLOWLOCATION to true.
Or set the CURLOPT_HEADER to true and then use regexp to get the Location header.

Resources