-3

I've a Api endpoint to work with code

<?php

    $name= $_POST['name'];
    $email = $_POST['email'];
    $password= $_POST['password'];
    $gender= $_POST['gender'];

    $con = mysqli_connect("localhost", "root", "qwerty", "db");
    $query= mysqli_query($con, "INSERT INTO users(name,email, password, gender) VALUES('$name','$email', '$password', '$gender')");

    if($query){
        echo "You are sucessfully Registered";
    }

    else{
        echo "your details could not be registered";
    }

    mysqli_close($con);

?>

how can I make the request should look like

{ 
   "name":"abc", 
   "email":"gmail", 
   "gender":"male" 
} 

I'm new to php Api development

Crazy Coder
  • 784
  • 2
  • 9
  • 24

2 Answers2

2

You have to build an array like so:

$data = array(
    'name' => $_POST['name'],
    'email' => $_POST['email'],
    'password' => $_POST['password']
);

And then you have to encode it : $encoded = json_encode($data)

Julien Lachal
  • 1,101
  • 14
  • 23
1
$data = array(
    'userID'      => 'a7664093-502e-4d2b-bf30-25a2b26d6021',
    'itemKind'    => 0,
    'value'       => 1,
    'description' => 'Boa saudaÁ„o.',
    'itemID'      => '03e76d0a-8bab-11e0-8250-000c29b481aa'
);

$json = json_encode($data);

$client = new Zend_Http_Client($uri);
$client->setRawData($json, 'application/json')->request('POST');

using this method you can send request in post

StudioTime
  • 22,603
  • 38
  • 120
  • 207
Shivangi Vyas
  • 117
  • 1
  • 9