Invalid method (HTTP Verb) is being used Error

Asked

Viewed 151 times

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.

inserir a descrição da imagem aqui

<?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.

  • I edited the post, see if you can understand better now.

  • improved but still not ideal, also post the code, to try to simulate your error,

  • I will edit and put the code for you to see

  • How do you send xml by post ? by your code you are opening the file, and working on it.

  • Exactly, I get an XML file from an API via post

  • After this returns the data, this message is being generated at the moment when the person sending the XML tries to communicate

  • 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.

Show 3 more comments

1 answer

0

I added the fear of sending the type $_POST[] so that when sending by POST he works on top of the temporary file.

I didn’t notice and understand all your script, but only in the middle of post, I defined the name as xml_post, just set the same name as your upload api.

<?php

/**
 * Lê o post enviado
 */

$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, $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); 

?>
  • Thanks for the return, above I was mistaken and said it was receiving a "file", but it is not file, it is only data via XML that are received via POST, there is no file.

  • I get it I’ll change then you test

  • Okay, I’m testing for Postman, in case it works out, I’ll get back to you.

  • I just did a test on Postman, but I still keep getting this error, yesterday I still contacted the support of Azure and I’m waiting for a measure. finally the error I get is this that I put in the post.

  • @Joãovictor runs a test in the first line of php var_dump($_POST['xml_post']); see if you will receive any output if you receive and any part of your code, if not and with your server

  • I made some changes here and debbuguei putting to record what is returning me, but now the problem is being in the return of the data, I will update the code for you see

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.