Pergunta

Oi eu sou tring para enviar alguns cabeçalhos no meu script PHP como

$headers[] = "BATCH_TYPE: XML_SINGLE"; 
$headers[] = "VENDOR_ID: 56309";

Mas eles estão sendo recebidos como:

Batch-Type Vendor-ID

.. não como eles foram destinados ou exigido -. Que está me causando problemas

Alguém sabe por que ou como classificar?

Obrigado,

<?php

function httpsPost($Url, $xml_data, $headers)
{
   // Initialisation
   $ch=curl_init();
   // Set parameters
   curl_setopt($ch, CURLOPT_FORBID_REUSE, 1); 
   curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
   curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
   curl_setopt($ch, CURLOPT_URL, $Url);
   // Return a variable instead of posting it directly
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLOPT_USERPWD,"username:password");

   // Activate the POST method
   curl_setopt($ch, CURLOPT_POST, 1) ;
   // Request
   curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);
   curl_setopt($ch, CURLOPT_TIMEOUT, 999);

   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
   curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

   // execute the connexion
   $result = curl_exec($ch);
   // Close it
   curl_close($ch);
   return $result;
}

$request_file = "./post_this.xml"; 
$fh = fopen($request_file, 'r'); 
$xml_data = fread($fh, filesize($request_file));

fclose($fh);    

$url = 'http://www.xhaus.com/headers';

$headers = array();
$headers[] = "Expect:";
$headers[] = "Accept: text/xml";

$headers[] = "BATCH_TYPE: XML_SINGLE"; 
$headers[] = "BATCH_COUNT: 1";
$headers[] = "VENDOR_ID: 54367";


$Response = httpsPost($url, $xml_data, $headers);

echo $Response;

?>
Foi útil?

Solução 4

Depois de uma semana de lutar com a companhia deste servidor externo, eles eram de fato me dar os cabeçalhos errados - D'oh

Outras dicas

Como você está verificando esses cabeçalhos? Acabei de me tentou com o seguinte código: -

<?php

header("BATCH_TYPE: XML_SINGLE");

E voltou o seguinte: -

HTTP/1.1 200 OK
Date: Wed, 10 Jun 2009 08:56:54 GMT
Server: Apache/2.2.11 (Ubuntu) PHP/5.2.6-3ubuntu4.1 with Suhosin-Patch
X-Powered-By: PHP/5.2.6-3ubuntu4.1
BATCH_TYPE: XML_SINGLE
Vary: Accept-Encoding
Content-Length: 0
Content-Type: text/html

Use fsockopen () e escrever / ler qualquer coisa manualmente, você necessidade. Eu não tenho certeza se a sua implementação de CURL ou smth. não mais como proxy não alterar seus cabeçalhos. Se você quer fazer algo bom com certeza - apenas fazê-lo você mesmo;). Na verdade, é tão fácil criar HTTP-solicitação e escrevê-lo para abrir soquete ...

$req = "POST $url HTTP/1.0\n";
$headers[] = 'VENDOR_ID: 1234';
$headers[] = 'MY_OTHER_HEADER: xxxxx';
$req .= implode("\n", $headers);
$req .= "\n\n" . $request_body;

$sock = fsockopen($host, 80, $errno, $errstr, $timeout);
fwrite($sock, $req, strlen($req));

$return = '';
do 
{
    $return .= fread($sock, 512);
} while (!feof($sock));

fclose($sock);

Não tenho certeza, mas parece-me, que smth. como isto já foi feito em algum lugar PERA ...

Alterar o código a seguir:

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

Para isto:

foreach($headers as $header)
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top