intestazioni HTTP Post, hanno ricevuto in modo diverso da ciò che è stato inviato
-
13-09-2019 - |
Domanda
Ciao io sono tring di inviare alcune intestazioni nel mio script PHP come
$headers[] = "BATCH_TYPE: XML_SINGLE";
$headers[] = "VENDOR_ID: 56309";
Ma essi vengono ricevuti come:
Batch-Type Vendor-ID
.. non come sono stati destinati o necessari -. Che è mi ha causato problemi
Qualcuno sa perché o come ordinare?
Grazie,
<?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;
?>
Soluzione 4
Dopo una settimana di combattere con la compagnia di questo server esterni, erano in realtà darmi le intestazioni sbagliate - D'oh
Altri suggerimenti
Come stai controllando queste intestazioni? Ho solo me stesso provato con il seguente codice: -
<?php
header("BATCH_TYPE: XML_SINGLE");
E tornato il seguente: -
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
fsockopen () e scrivere / leggere manualmente qualsiasi cosa bisogno. Io non sono sicuro se l'implementazione di CURL o smth. altro delega come non modifica le intestazioni. Se si vuole fare qualcosa di buono per certo - basta farlo da soli;). In realtà è così facile creare HTTP-request e scriverlo alla presa aperto ...
$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);
Non sono sicuro, ma mi sembra che si dovrebbe occupare. come questo è già stato fatto da qualche parte nel PEAR ...
Modificare il seguente codice:
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
Per questo:
foreach($headers as $header)
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);