简体   繁体   English

没有使用curl获取个人数据

[英]Not getting individual data using curl

i am trying to get indivdual data using curl from this website http://dummy.restapiexample.com/ .. I use the following code to get all data 我试图使用curl从此网站http://dummy.restapiexample.com/获取个人数据。我使用以下代码获取所有数据

<?php
$url='http://dummy.restapiexample.com/api/v1/employees';
$my_curl = curl_init();
curl_setopt($my_curl, CURLOPT_URL, $url);
curl_setopt($my_curl, CURLOPT_BINARYTRANSFER, true);
curl_setopt($my_curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($my_curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($my_curl, CURLOPT_SSL_VERIFYPEER, 0);
$my_data = curl_exec($my_curl);
$my_json = json_decode($my_data);
echo '<pre>'; print_r($my_json ); echo '</pre>';
?>

1) This is working . 1)这是工作。 But i want to get the indvidual data , so i tried to chage url and use the same code 但是我想获取单个数据,所以我尝试修改url并使用相同的代码

$url="http://dummy.restapiexample.com/api/v1/employee/1"

But it is not working . 但这是行不通的。

2) Also i try to create a new record , that is working 2)我也尝试创建一个新的记录, 正在工作

$data = array("name" => "Hagridbo", "salary" => "123",  "age" => "36");                                                                    
$data_string = json_encode($data);                                                                                   

$ch = curl_init('http://dummy.restapiexample.com/api/v1/create');                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string))                                                                       
);                                                                                                                   

$result = curl_exec($ch);

But how can i get the response like data is inserted ? 但是我如何获得插入数据的响应? what data is inserted ? 插入什么数据?

1. Regarding individual employee data is not coming 1.关于个人员工数据不来了

If you look to the http://dummy.restapiexample.com/api/v1/employees , you will see that employee id 1 is not there. 如果您查看http://dummy.restapiexample.com/api/v1/employees ,则会看到没有员工ID 1。 That's why you are not getting any data. 这就是为什么您没有任何数据的原因。

for example check data of this link:- http://dummy.restapiexample.com/api/v1/employee/20276 例如,检查此链接的数据: -http : //dummy.restapiexample.com/api/v1/employee/20276

Also it seems that v1/employees API data changing on a regular interval. 同样, v1/employees API数据似乎定期更改。

2. Regarding created an employee successfully, but not getting any response. 2.关于成功创建员工,但未得到任何回应。

do var_dump($result); var_dump($result); after $result = curl_exec($ch); 之后$result = curl_exec($ch); and see what it give you. 看看它能给你什么。 I think it will give you a json data response of added user. 我认为它将为您提供添加用户的json数据响应。

I said it based on details of that API link, check here: http://dummy.restapiexample.com/create 我是根据该API链接的详细信息说的,请在此处查看: http : //dummy.restapiexample.com/create

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM