Manik
Manik

Reputation: 523

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

<?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 . But i want to get the indvidual data , so i tried to chage url and use the same code

$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

$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 ?

Upvotes: 0

Views: 100

Answers (1)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72299

1. Regarding individual employee data is not coming

If you look to the http://dummy.restapiexample.com/api/v1/employees, you will see that employee id 1 is not there. That's why you are not getting any data.

for example check data of this link:- http://dummy.restapiexample.com/api/v1/employee/20276

Also it seems that v1/employees API data changing on a regular interval.

2. Regarding created an employee successfully, but not getting any response.

do var_dump($result); after $result = curl_exec($ch);and see what it give you. I think it will give you a json data response of added user.

I said it based on details of that API link, check here: http://dummy.restapiexample.com/create

Upvotes: 1

Related Questions