Wednesday, 18 February 2015

How to send push Notification to iPhone from PHP (web services/API)

Hello Developers,
   If you want to send push notification from php to iPhone application then create a new file with iphone_push.php and save it in your api folder. Paste following code in file :

 <?php
function pushnofitication($deviceToken, $message)
{

// Put your device token here (without spaces):
$deviceToken = $deviceToken;

// Put your private key's passphrase here:
$passphrase = '12345';

// Put your alert message here:
$message = $message;

////////////////////////////////////////////////////////////////////////////////

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'apns_cert.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

// Open a connection to the APNS server
$fp = stream_socket_client(
'ssl://gateway.sandbox.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);

// Create the payload body
$body['aps'] = array(
'alert' => 
array (
"body" => $message,
),
'sound' => 'default',
);

// Encode the payload as JSON
$payload = json_encode($body);

// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;

// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));

/*if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;
*/
// Close the connection to the server
fclose($fp);
}
?>

For this you need a .pem file. Get this file from your iphone developer and save in your folder with name apns_cert.pem. While create pem file firstly they create .cert file. After create .cert file they need to enter a passphrase to create .pem file. so also you need to change passphrase.

The Above code is for sandbox mode when you app is live then replace .pem file and change url with live so your push notification is also work with live app.

When you want to send push notification include this file in your file and call function :
pushnofitication($deviceToken, $message)
$deviceToken : device token in unique id of iphone so when you register user get this token and save it in database. When you send push notification get this token from database and pass in this function.
$message : Message which you want to show in notification.

Thanks & Regard
Pulkit Jalani
http://www.webtecso.com

Sunday, 8 February 2015

How to add order in Woocommerce From PHP Web Services / Web Apis

Hello Developers,
   If you want to add order of woocommerce from php web api then you can create a file in your api folder named : add_order.php
Paste following code in your add_order.php file :

<?php
include ('../wp-load.php');

$order_data = array(
    'post_name'     => 'order-' . date_format($order_date, 'M-d-Y-hi-a'), //'order-jun-19-2014-0648-pm'
    'post_type'     => 'shop_order',
    'post_title'    => 'Order &ndash; ' . date_format($order_date, 'F d, Y @ h:i A'), //'June 19, 2014 @ 07:19 PM'
    'post_status'   => 'wc-completed',
    'ping_status'   => 'closed',
    'post_excerpt'  => $_REQUEST['note'],
    'post_date'     => date_format($order_date, 'Y-m-d H:i:s e'), //'order-jun-19-2014-0648-pm'
    'comment_status' => 'open'
);

// create order
$order_id = wp_insert_post( $order_data, true );
$order->imported = true;

 // get product by item_id
    $product = $_REQUEST['product_id'];


if( $product ) {

        // add item
        $item_id = wc_add_order_item( $order_id, array(
            'order_item_name'       => {Your Item name, which you can fetch from product post type with product id},
            'order_item_type'       => 'line_item'
        ) );

        if ( $item_id ) {

            // add item meta data
            wc_add_order_item_meta( $item_id, '_qty', $_REQUEST['quantity'] ); 
            wc_add_order_item_meta( $item_id, '_product_id',$product );

        }

        // set order status as completed
        wp_set_object_terms( $order_id, 'completed', 'shop_order_status' );


    } else {

        $order->errors = 'Product SKU (' . $order->$item_id . ') not found.';
    }
?>

After you can pass all parameter with below api link
http://www.webtecso.com/api/add_order.php

Wednesday, 4 February 2015

How to Update Blog content in WordPress from Web Services (API)

Hello Developers,
  If you want to update your blog content with web api then you create a new file with name update_blog.php in your api folder and paste following code :

<?php
require ("../wp-load.php");
if ($_REQUEST['id'] != "") {
global $wpdb;

$my_post = array(
 'ID'           => $_GET['id'],
                 "title"       => $_GET['title'],
 );

// Update the post into the database
 wp_update_post( $my_post );

$ar = array('reg'=>'true','msg'=>'success');
echo json_encode( $ar );
exit();
} else {
$ar = array ("reg" => 'false', "msg" => "Require Field Missing");
echo json_encode( $ar );
exit();
}
?>

You can added as many all argument of post in $my_post array and save your blog post, its required post id for update post. Now you api url is as follows :

http://www.webtecso.com/api/update_blog.php?id=

Monday, 2 February 2015

How to Display post content of WordPress from Web Services (API)

Hello Developers,
   If you want to display the content of your blog post in your app. Then you need to create a php page with names display_blog.php (you can change this name) in your previous API folder and paste below code :

<?php

require ("../wp-load.php");

$args = array( 
'post_type' => "post",
'author' => $_GET['user_id'],
'post_status' => 'publish',
'posts_per_page' => -1,
);



// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) : $the_query->the_post();
$b[] = array(
"id" => get_the_ID(),
"name" => get_the_title(),
);
endwhile;
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();

echo json_encode ($b);

?>

You can also pass user id in url so you can fetch blog post according to user or you can remove this line :
'author' => $_GET['user_id'],

Then you need to call this url :
www.webtecso.com/api/display_blog.php?usser_id={Your user id}