Wednesday, 7 January 2015

How to Create Registration Web Service (API) in Wordpress

Hello Developers,
   Sometimes we want to register user from Iphone or Android Application. for that you can create a file in your root directory

like

create a folder named "api" in your root directory
and create "register.php" file in this folder
then your url like this www.webtecso.com/api/register.php

for registration you need to pass argument

  • Name
  • Email
  • Password
  • Username (if your username same as email then no need to pass this parameter)
then your PHP script is look like below :-

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

$device_id = $_REQUEST['device_id'];

$name = $_REQUEST['names'];
$user_login = $_REQUEST['emails'];
$user_email = $_REQUEST['emails'];
$user_pass = $_REQUEST['password'];

$userdata = compact('name', 'user_login', 'user_email', 'user_pass');
$user_id = wp_insert_user( $userdata );

if ( $_FILES['image_url']['name'] != "" ) {
global $wpdb;
//For Uploading photo from front End
if (!function_exists('wp_generate_attachment_metadata')){
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
}
$overrides = array( 'test_form' => false);
$file = wp_handle_upload($_FILES['image_url'], $overrides);
$file['url'];

$content = $file['url'];
}

update_user_meta( $user_id, "user_pic_url", $content );


$ar = array('reg'=>'true','msg'=>'success', "id" => $user_id);
echo json_encode( $ar );
exit();

} else {
$ar = array ("reg" => 'false', 'false' => "registration failed: Sorry, that username already exists!");
echo json_encode( $ar );
exit();
}
} else {
$ar = array ("reg" => 'false', "msg" => "Require Field Missing");
echo json_encode( $ar );
exit();
}
?>

this will take url as follows :-
www.webtecso.com/api/register.php?names=&emails=&password=

then return error or success.
if your successfully registered then its return user id in JSON format
If there is any error then its return error in JSON format

No comments:

Post a Comment