Created
May 14, 2020 20:24
-
-
Save kritschy/9e33d06c5005769768e2bd9c89b69666 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$service_url = 'yourdomain.org/api/books'; | |
$curl = curl_init($service_url); | |
$curl_post_data = array( | |
'name' => $_POST['title'], | |
'description' => $_POST['description'] | |
); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($curl, CURLOPT_HTTPHEADER, array( | |
'Authorization: Token ' | |
)); | |
curl_setopt($curl, CURLOPT_POST, true); | |
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data); | |
$curl_response = curl_exec($curl); | |
if ($curl_response === false) { | |
$info = curl_getinfo($curl); | |
curl_close($curl); | |
die('error occured during curl exec. Additional info: ' . var_export($info)); | |
} | |
curl_close($curl); | |
$decoded = (array) json_decode($curl_response, true); | |
echo($decoded["id"]); | |
echo('</br>'); | |
echo($_POST["category"]); | |
process_book($decoded["id"], $_POST["category"]); | |
if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') { | |
die('error occured: ' . $decoded->response->errormessage); | |
} | |
echo '<h1>Vielen Dank</h1>'; | |
var_export($decoded->response); | |
function process_book($id, $category) { | |
$id = strval($id); | |
$url = 'yourdomain.org/api/shelves/' . '{' .$id .'}'; | |
$curl = curl_init($url); | |
$update_date = "'books': [$id]"; | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT"); | |
curl_setopt($curl, CURLOPT_HTTPHEADER, array( | |
'Authorization: Token ' | |
)); | |
curl_setopt($curl, CURLOPT_POSTFIELDS, $update_date); | |
$shelf_list = curl_exec($curl); | |
if ($shelf_list === false) { | |
$info = curl_getinfo($curl); | |
curl_close($curl); | |
die('error occured during curl exec. Additional info: ' . var_export($info)); | |
} | |
if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') { | |
die('error occured: ' . $decoded->response->errormessage); | |
} | |
curl_close($curl); | |
} | |
?> | |
` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment