How to set custom REQUEST header in PHP

后端 未结 1 1292
暗喜
暗喜 2020-12-21 11:09

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

Thanks in advance!

相关标签:
1条回答
  • 2020-12-21 11:53

    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; 
    
    0 讨论(0)
提交回复
热议问题