0
The page you are Looking for cannot be displayed because an invalid method (HTTP Verb) is being used.
This error is being presented, when I try to send via POST
data XML
to an archive PHP
.
I wrote a program in PHP
who receives a request via POST
data XML
and do a re-sponse.
When sending is done by calling this program, this mentioned error happens.
<?php
/**
* Lê o post enviado
*/
$dataPOST = trim(file_get_contents('php://input'));
/**
* Captura o conteúdo XML
*/
$xmlData = simplexml_load_string($dataPOST);
/**
* Gravando conteudo no arquivo texto.
*/
$myFile = "data_backup.xml";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $xmlData;
fwrite($fh, $stringData);
/** ENVIO *****************************************************************/
/**
* Definir a URL POST e também payload
*/
date_default_timezone_set('America/Sao_Paulo');
$timeStampGMT = time()+date("Z");
$timeStampGMT = gmdate("d/m/Y H:i:s",$timeStampGMT);
define('XML_POST_URL', 'http://cody.glpconnect.com');
$post_string = '<?xml version="1.0" encoding="UTF-8"?><state>ok</state>';
// Opções do header
$header = "POST HTTP/1.0 \r\n";
$header .= "Content-type: text/xml \r\n";
$header .= "Content-length: ".strlen($post_string)." \r\n";
$header .= "Content-transfer-encoding: text \r\n";
$header .= "Connection: close \r\n\r\n";
$header .= $post_string;
/**
* Inicializar o identificador e definir opções
*/
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, XML_POST_URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);
/**
* Verifica se há erros
*/
if ( curl_errno($ch) ) {
$result = 'cURL ERROR -> ' . curl_errno($ch) . ': ' . curl_error($ch);
} else {
$returnCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
switch($returnCode){
case 200:
break;
default:
$result = 'HTTP ERROR -> ' . $returnCode;
break;
}
}
/**
* fecha o handle
*/
curl_close($ch);
/**
* Saída dos resultados e do handle
*/
fwrite($fh, $result);
fclose($fh);
/**
* Saida do script
*/
exit(0);
?>
------------Edited ---------
/**
* Lê o post enviado
*/
$dataPOST = trim(file_get_contents('php://input'));
//$dataPOST = trim(file_get_contents($_POST['xml_post']));
/**
* Captura o conteúdo XML
*/
$xmlData = simplexml_load_string($dataPOST);
/**
* Gravando conteudo no arquivo texto.
*/
$myFile = "data_backup.xml";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $xmlData;
fwrite($fh, "Data POST:\r\n");
fwrite($fh, $dataPOST);
fwrite($fh, "\r\n");
fwrite($fh, "STRING DATA:\r\n");
fwrite($fh, var_dump($stringData));
/** ENVIO *****************************************************************/
/**
* Definir a URL POST e também payload
*/
date_default_timezone_set('America/Sao_Paulo');
$timeStampGMT = time()+date("Z");
$timeStampGMT = gmdate("d/m/Y H:i:s",$timeStampGMT);
define('XML_POST_URL', 'http://cody.glpconnect.com');
$post_string = '<?xml version="1.0" encoding="UTF-8"?><state>ok</state>';
// Opções do header
$header = "POST HTTP/1.0 \r\n";
$header .= "Content-type: text/xml \r\n";
$header .= "Content-length: ".strlen($post_string)." \r\n";
$header .= "Content-transfer-encoding: text \r\n";
$header .= "Connection: close \r\n\r\n";
$header .= $post_string;
/**
* Inicializar o identificador e definir opções
*/
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, XML_POST_URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);
/**
* Verifica se há erros
*/
if ( curl_errno($ch) ) {
$result = 'cURL ERROR -> ' . curl_errno($ch) . ': ' . curl_error($ch);
} else {
$returnCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
switch($returnCode){
case 200:
break;
default:
$result = 'HTTP ERROR -> ' . $returnCode;
break;
}
}
/**
* fecha o handle
*/
curl_close($ch);
/**
* Saída dos resultados e do handle
*/
fwrite($fh, "\r\n");
fwrite($fh, "Resultado:\r\n");
fwrite($fh, "\r\n");
fwrite($fh, $result);
fclose($fh);
/**
* Saida do script
*/
exit(0);
?>
I don’t understand your problem.
– Bulfaitelo
I edited the post, see if you can understand better now.
– João Victor
improved but still not ideal, also post the code, to try to simulate your error,
– Bulfaitelo
I will edit and put the code for you to see
– João Victor
How do you send xml by post ? by your code you are opening the file, and working on it.
– Bulfaitelo
Exactly, I get an XML file from an API via post
– João Victor
After this returns the data, this message is being generated at the moment when the person sending the XML tries to communicate
– João Victor
then this code of yours is not treating a post submission. I will change it to receive this file by post so you test it.
– Bulfaitelo