0
I’m making a webservice, without any framework, using PHP+JSON, with localhost xampp initially, so I’ll put it on some cloud server.
I created in xampp/htdocs two folders, "client" and "vendor", in the future 'client' will be on one server and 'vendor' in another, where in client will be the access of the user sending data through a form via Curl, and the supplier will receive this data and provide information for return in the customer interface. For now I am not using database, but soon I will complete, I believe that for this initial mechanism is not necessary bd.
To process the form data sent by the client, I did it as follows:
$url = "http://127.0.0.1/ws/fornecedor/wsClientRequire.php";
$cURL = curl_init($url);
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
// Definimos um array seguindo o padrão:
// '<name do input>' => '<valor inserido>'
$dados = array(
'nome' => 'John da silva',
'email' => '[email protected]',
'mensagem' => 'Testando o cURL!'
);
// Iremos usar o método POST
curl_setopt($cURL, CURLOPT_POST, true);
// Definimos quais informações serão enviadas pelo POST (array)
curl_setopt($cURL, CURLOPT_POSTFIELDS, $dados);
$resultado = curl_exec($cURL);
$resposta = curl_getinfo($cURL, CURLINFO_HTTP_CODE);
curl_close($cURL);
if ($resposta == '404') {
echo 'O site está fora do ar (ERRO 404)!';
} else {
echo 'Parece que está tudo bem...';
}
//var_dump(json_decode($jsonRet, true)); //com var_dump aparece NULL
$retorno = json_decode($jsonRet);
$success = $retorno->data->success;
echo " \n success: ".$success;
In this passage:
$retorno = json_decode($jsonRet);
$success = $retorno->data->success;
echo " \n success: ".$success;
I was hoping to receive the information contained in the supplier’s file... but that didn’t happen... Then I would like to know how to do this part of the web service configuration. I did so at the vendor:
$success = "Recebido com sucesso.";
$message = null;
$dados = array();
header('Content-Type: application/json; charset=utf-8');
echo json_encode(
array(
'success' => $success,
'message' => $message,
'dados' => $dados
)
);
exit;
The communication between customer and supplier did not take place as desired... someone knows how to solve???
To clarify my question... in the "supplier" that is the last code I posted, how do I read the data that the customer sent via Curl? and how do I make a return available, which will be presented in the customer interface? As I send headers via Curl in the "client" file, in this headers in the Curl setopt it would be necessary to send a kind of token for access authorization, and how do I read this header token in the vendor file?
you have already tested the supplier and it is accessible? Or really your question is how to get the data sent from the customer?
– girorme
Hi @Turqspl I was able to make the communication. I needed to put a $_POST['name'] on the supplier in order to receive customer data. Now my still pending doubt, is how to send the headers via customer’s Curl to the supplier, in the headers would need to be sent a token type for use authorization.
– Neo
When I set header, like this: curl_setopt($Curl, CURLOPT_HTTPHEADER, array( 'APIKEY: 111111111111111111111111111', 'Content-Type: application/json', /$headers )); * So $_POST doesn’t work...
– Neo
Have you tried using
file_get_contents('php://input')
?– Gabriel Heming
Hi @Gabrielheming managed to capture the data.. now I need to know how to do header authorization
– Neo
...cmo read header Authorization on vendor part
– Neo
There are some ways to grab the headers, the most common is by using apache_request_headers. Also validate if the $_SERVER variable is not coming with the header. It is also possible to add via HTACCESS.
– Gabriel Heming
I will test apache_request_headers... when I put CURLOPT_HTTPHEADER on the customer, the supplier no longer receives the data.. I need to know how to use Authorization and the supplier can receive the data via $_POST
– Neo
in the client I put like this; curl_setopt($Curl, CURLOPT_HTTPHEADER, array( "Content-Type: application/json", ); ...and at the vendor like this: $headers = apache_request_headers() ...but no data or headers post is coming back or entering in the BD... some Uthorization must be missing in this implementation
– Neo
and you know how to take a specific data with the $headers = apache_request_headers() ?
– Neo