Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set custom REQUEST header in PHP

Tags:

php

In creating RESPONSE headers, using header() function will do the trick. How about for request headers?

Thanks in advance!

like image 506
Andrew Festin Avatar asked Aug 25 '26 14:08

Andrew Festin


1 Answers

If you are talking about making a request and specifying your headers, one way to do it is using CURL:

$data = "<soap:Envelope>[...]</soap:Envelope>"; 
$tuCurl = curl_init();
curl_setopt($tuCurl, CURLOPT_HEADER, 0);
curl_setopt($tuCurl, CURLOPT_URL, "http://example.com/path/for/soap/url/");
curl_setopt($tuCurl, CURLOPT_POST, 1); // if post request
curl_setopt($tuCurl, CURLOPT_POSTFIELDS, $data);
curl_setopt($tuCurl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($tuCurl, CURLOPT_HTTPHEADER, array("Content-Type: text/xml","SOAPAction: \"/soap/action/query\"", "Content-length: ".strlen($data)));
$tuData = curl_exec($tuCurl); 
if(!curl_errno($tuCurl)){ 
  $info = curl_getinfo($tuCurl); 
  echo 'Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url']; 
} else { 
  echo 'Curl error: ' . curl_error($tuCurl); 
} 
curl_close($tuCurl); 
echo $tuData; 
like image 99
i-- Avatar answered Aug 28 '26 03:08

i--



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!