如果在php

时间:2016-10-18 05:00:25

标签: php mysql api

我一直在试图弄清楚如何捕获使用API​​的网站的整个网址。我有一个脚本使用API​​传递数据,我也有一个处理发布数据的API。插入有效,但它在我的数据库中插入空数据。

这是我的API

<?php
$link = mysqli_connect('localhost', 'root', '', 'datauser');
mysqli_set_charset($link,'utf8');

$method = strtolower(array_shift($url_send));

if ($method = 'post') {
$id = $_GET['id'];
$name = $_GET['name'];
$cell = $_GET['cell'];
}



switch ($method) {
case 'get':
  //$sql = "select * from users WHERE user_lname= '$name'"; break;
case 'put':
  //$sql = "update users set $set where user_lname=$name"; break;
case 'post':
  $sql = "insert into users (user_fname, user_lname, user_contact) values ('$name', '$name', '$cell')"; break;
case 'delete':
  //$sql = "delete users where id=$key"; break;
}

$result = mysqli_query($link,$sql);

if ($method == 'post') {
echo mysqli_insert_id($link);
}

die();

现在这是我使用上述API的代码

<?php
$request = explode('/', trim($_SERVER['PATH_INFO'],'/'));

$data = array(
"table" => $request[0],
"FNAME" => $request[1],
"LNAME" => $request[2],
"CNTCT" => $request[3]
);

$url_send ="http://localhost/api.php?   id=".$request[0]."&name=".$request[1]."&cell=".$request[3];
$str_data = json_encode($data);
$post_elements = array('data'=>$data);


function sendPostData($url){
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query($url_send));
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch, CURLOPT_HEADER, 0);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec( $ch );
curl_close($ch);
//print_r($response);
 return $response;
}

 echo sendPostData($url_send);

?>

我收到的错误就是这个错误

**
Notice: Undefined index: PATH_INFO in C:\xampp\htdocs\api.php on line 5

Notice: Undefined index: id in C:\xampp\htdocs\api.php on line 9

Notice: Undefined index: name in C:\xampp\htdocs\api.php on line 10

Notice: Undefined index: cell in C:\xampp\htdocs\api.php on line 11
21**

21 是我最近插入的ID。插入有效但似乎我实际上无法从URL中获取数据。

有人,请帮忙。非常感谢任何建议,甚至评论否定的。

感谢。

0 个答案:

没有答案