How do I view response codes when posting to an API using a PHP function?
How do I view response codes when posting to an API using a PHP function?
I hope someone can give me some good guidelines because I cannot find proper info online. I've been following the instructions of an API's Documentation. When there is an order made in WordPress, I want to post my data to an external API. For the most part everything is going well. However, I don't understand how I'm supposed to see response codes?
For example, I'm supposed to be getting a response like this:
{
"response":52
}
I did a lot of googling, and it sounds like most http responses are shown inside the browser's developer tools. However, it sounds like in my particular case, this is server side. Are these responses logged on the server? Am I supposed to add any code that helps me see responses during testing?
Here's an example of a basic code I'm using for the API:
<?php
add_action( 'woocommerce_new_order', 'create_new_external_order', 10, 2 );
function create_new_external_order ( $order_id, $order ){
function sendRequest($url, $type = 'get', $params = [], $json = true) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
if ($type == 'post' || $type == 'put') {
curl_setopt($ch, CURLOPT_POST, true);
if ($json) {
$params = json_encode($params);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Content-Length: ' . strlen($params)
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
} else {
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
}
}
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_USERAGENT, 'Poster (http://my-api-base-url.com)');
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$url = 'https://my-api-base-url.com/api/blahblahblah'
. '?format=json'
. '&token=xxxxxxxxx';
$incoming_order = [
'spot_id' => 1,
'phone' => '+0123456789',
'products' => [
[
'product_id' => 1,
'count' => 5
],
],
];
$data = sendRequest($url, 'post', $incoming_order);
}
?>
To handle response codes from the external API within your PHP code, you can modify the sendRequest function to capture and handle HTTP response codes. Here's an updated version of your code with comments explaining the changes:
function sendRequest($url, $type = 'get', $params = [], $json = true) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
if ($type == 'post' || $type == 'put') {
curl_setopt($ch, CURLOPT_POST, true);
if ($json) {
$params = json_encode($params);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Content-Length: ' . strlen($params)
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
} else {
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
}
}
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_USERAGENT, 'Poster (http://my-api-base-url.com)');
// Execute the request and capture both response data and HTTP response code
$data = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// Close the cURL session
curl_close($ch);
// Return both response data and HTTP response code
return ['data' => $data, 'http_code' => $http_code];
}
// Usage example:
$response = sendRequest($url, 'post', $incoming_order);
// Check the HTTP response code
if ($response['http_code'] == 200) {
// Successful request
$responseData = json_decode($response['data'], true);
// Process $responseData as needed
} else {
// Handle the error
echo "Error: HTTP response code " . $response['http_code'];
}
With this modification, the sendRequest function now returns an array containing both the response data and the HTTP response code. In your main code, you can then check the HTTP response code and handle any errors accordingly.
February 13, 2024
Turbo Multi-language Translator
Make the better internet purchasing globaly
Turbosify SEO Speed Booster
5.0 (7)•Free plan available
Get better conversions by improving store loading speed
Installed
Turbo Multi-language Chat - AI Customer service in one hand
February 13, 2024