dongrong5189 2016-10-20 01:30
浏览 25

在API中捕获调用

I have a page wherein it will serve as a for that is connected to my API.php. In this page I used Curl PUT verb and passed the URL and the data I need to pass which is in the form of an array.

I tried catching the response in the side of the API but to no avail.

Here's my code.

The URL they need to populate or I think this is the API

http://localhost/sample/index.php/2/users/mil/milastname/09876543212

Legend:

Sample - is the folder where I stored my index.php

2 - is the action. In this case 2 stands for PUT verb.

users - is the table in the database which I want to update.

mil - is my firstname

milastname - is my lastname

09876543212 - is my mobile

MY FORMS

index.php

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


if ($request[0] == 2) {
$data = array(
  "table" => $request[1],
  "fname" => $request[2],
  "lname" => $request[3],
  "cntct" => $request[4]
);

$url_send ="http://localhost/api.php";
$str_data = json_encode($data);
$post_elements = array('data'=>$data);

if ($request[0] == 2) {
  put($url_send, $post_elements);
  echo put($url_send, $post_elements);
}


function put($url, $fields)
{

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");

curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query($fields));

echo curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query($fields));

$response = curl_exec($ch);

curl_close ($ch);

return $response;
}

Here's my API.php

if ($method == 'PUT') {
$data = preg_replace('/[^a-z0-9_]+/i','',array_shift($_POST));
echo $data;
die();
}

switch ($method) {
case 'PUT':
  $sql = "update `$table` set $set where id=$key"; break;
case 'POST':
  $sql = "insert into `$table` (user_fname, user_lname, user_contact) values ('$fname', '$lname', '$cntct')"; break;
case 'DELETE':
  $sql = "delete `$table` where id=$key"; break;
}

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

In the part of if ($method == 'PUT')

I tried inserting an echo and a die to stop the process to that I can see the array So I can arrange the update query in my method case update. But it returns no result.

Thanks for the help and sorry for the long post. Need to detail it so that I can explain my question clearly.

Thanks.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 HLs设计手写数字识别程序编译通不过
    • ¥15 Stata外部命令安装问题求帮助!
    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向
    • ¥15 如何用python向钉钉机器人发送可以放大的图片?
    • ¥15 matlab(相关搜索:紧聚焦)
    • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
    • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
    • ¥50 需求一个up主付费课程
    • ¥20 模型在y分布之外的数据上预测能力不好如何解决