Php RESTAPI对URL重写的响应
我正在使用PHP尝试RESTAPI。我已使用.htaccess规则修改了URL重写。是从curl代码和数据数组一起发送的POST URL。因此,php将其作为POST命令读取,并使用PDO将数据插入到我的products表中 这是我的curl命令代码:Php RESTAPI对URL重写的响应,php,.htaccess,rest,curl,pdo,Php,.htaccess,Rest,Curl,Pdo,我正在使用PHP尝试RESTAPI。我已使用.htaccess规则修改了URL重写。是从curl代码和数据数组一起发送的POST URL。因此,php将其作为POST命令读取,并使用PDO将数据插入到我的products表中 这是我的curl命令代码: <?php $data=array( 'product_name' =>'Television', 'price' => 1000, 'quantity' => 10,
<?php
$data=array(
'product_name' =>'Television',
'price' => 1000,
'quantity' => 10,
'seller' =>'XYZ Traders'
);
$url = 'http://localhost/API2/products';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response_json = curl_exec($ch);
curl_close($ch);
$response=json_decode($response_json, true);
echo $response;
?>
而products.php的php代码是:
<?php
// Connect to database
require_once('database.php');
$request_method=$_SERVER["REQUEST_METHOD"];
switch($request_method)
{
case 'POST':
// Insert Product
insert_product($db);
break;
default:
// Invalid Request Method
header("HTTP/1.0 405 Method Not Allowed");
break;
}
function insert_product($db)
{
//global $connection;
$_product_name=$_POST["product_name"];
$_price=$_POST["price"];
$_quantity=$_POST["quantity"];
$_seller=$_POST["seller"];
$query = 'INSERT INTO products
(Product_name, Price, Quantity,Seller)
VALUES
( :product_name, :price, :quantity,:seller)';
$statement = $db->prepare($query);
//$statement->bindValue(':category_id', $category_id);
$statement->bindValue(':product_name', $_product_name);
$statement->bindValue(':price', $_price);
$statement->bindValue(':quantity', $_quantity);
$statement->bindValue(':seller', $_seller);
$bool=$statement->execute();
$statement->closeCursor();
if($bool)
{
$response=array(
'status' => 1,
'status_message' =>'Product Added Successfully.'
);
}
else
{
$response=array(
'status' => 0,
'status_message' =>'Product Addition Failed.'
);
}
header('Content-Type: application/json');
echo json_encode($response);
}
//$db = null;
// Close database connection
//mysqli_close($connection);