prmas prmas - 6 months ago 79
PHP Question

send response HTTP 200 SUCCESS in php

I need to send an HTTP 200 response to the string 'SUCCESS', but my php server version is 5.2.17!

In my case, the webhook sends data to the capture to a file called notification.php, I read the contentes, save in the database and need to send a response but do not know how to do this!

Does anyone know how to do this in php 5.2.17?

I tried the following ways without success:

// error 1
header("Content-Type: text/plain");
echo "SUCCESS";

// error 2
$httpStatusCode = 200;
$httpStatusMsg = 'SUCCESS';
$protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0';
header($protocol.' '.$httpStatusCode.' '.$httpStatusMsg);

// error 3
header("200 SUCCESS");
return "200 SUCCESS";

// error 4
header('Content-Type: application/json');
echo 'SUCCESS';

// error 5
header('Content-Type: application/json');
return 'SUCCESS';

//error 6
header('Content-Type: application/json');
header('SUCCESS');

///error 7
header('Content-Type: application/json');
$success =json_encode('SUCCESS');
header($success);


///error 8
header("HTTP/1.1 200 SUCCESS");
header("Content-Type:application/json; charset=utf-8");

///error 9
header("HTTP/1.1 200");
header("Content-Type:application/json; charset=utf-8");
result 'SUCCESS';

//error 10
header("Content-Type:application/json;");
header('HTTP/1.0 200 SUCCESS');

// error 11
$code = 200;
$text = 'OK';
$protocol = (isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0');
header($protocol . ' ' . $code . ' ' . $text);
$GLOBALS['http_response_code'] = $code;
echo 'SUCCESS';


My PhpCode

<?php
$src_data = $_REQUEST["data"];
$une_data = stripslashes($src_data);
$data = json_decode($une_data);

//get payment data
$id_cob = $data->payment->id;
$id_cus = $data->payment->customer;
$status = $data->payment->status;
$dtsts = date("Y-m-d");

if ($data->event == 'PAYMENT_RECEIVED') {
// post client
include('dbconnection.php');
$qryn = "UPDATE PAYMENTS SET STATUS='$status', DATASTATUS='$dtsts' WHERE ID_COB_ASAAS='$id_cob' AND ID_CLI_ASAAS='$id_cus'";
mysql_query($qryn,$cnx);
}

// webhook return
// my solution was:
header('HTTP/1.1 200 OK');
echo 'SUCCESS';
return;

?>

Answer

Try:

$code = 200;
$text = 'OK';
$protocol = (isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0');
header($protocol . ' ' . $code . ' ' . $text);
$GLOBALS['http_response_code'] = $code;
echo 'SUCCESS';
Comments