php - 修改PHP/SOAP代码,在所有请求中添加HTTP Header

标签 php http soap soap-client

我继承了一些 php SOAP 代码,由于我们使用的服务发生了变化,我需要修改为“在所有请求的 HTTP header 中添加授权”。我不确定该怎么做,如果可能的话。

部分相关代码如下所示:

    function soap_connect() {
            $soap_options = array(
                    'soap_version' => SOAP_1_2,
                    'encoding' => 'UTF-8',
                    'exceptions' => FALSE
            );
            try {
                    $this->soap_client = new SoapClient($this->configuration['wsdl'], $soap_options);
            } catch (SoapFault $fault) {
                    return FALSE;
            }
            return TRUE;
    }

我认为,据我了解,它应该只是输出以下内容(现在):

Content-Type: application/soap+xml;charset=UTF-8;action="http://ws.testserver.net/nsp/client/hsserve/listHardware"
Content-Length: 255
...

documentatino 说最终的 HTTP 请求应该是这样的:

Content-Type: application/soap+xml;charset=UTF-8;action="http://ws.testserver.net/nsp/client/hsserve/listHardware"
Authorization: WRAP access_token=Z-H7SnqL49eQ2Qp5pLH8k-RVxHfewgIIDt4VCeI2CNnrS4-gBMzPWbfZuMhgvISVV-uTSikS1SqO0n2PRkH3ysQ-uWbvU9podPAm6HiiIS5W2mtpXUfN9ErBmkjF6hDw
Content-Length: 255

最佳答案

添加流上下文以向 HTTP 调用提供额外的 header 。

function soap_connect() {
    $context = array('http' =>
        array(
            'header'  => 'Authorization: WRAP access_token=Z-H7SnqL49eQ2Qp5pLH8k-RVxHfewgIIDt4VCeI2CNnrS4-gBMzPWbfZuMhgvISVV-uTSikS1SqO0n2PRkH3ysQ-uWbvU9podPAm6HiiIS5W2mtpXUfN9ErBmkjF6hDw'
        )
    );
    $soap_options = array(
        'soap_version' => SOAP_1_2,
        'encoding' => 'UTF-8',
        'exceptions' => FALSE,
        'stream_context' => stream_context_create($context)
    );
    try {
        $this->soap_client = new SoapClient($this->configuration['wsdl'], $soap_options);
    } catch (SoapFault $fault) {
        return FALSE;
    }
    return TRUE;
}

请参阅SoapClient::__construct()HTTP context options如需更多信息,

关于php - 修改PHP/SOAP代码,在所有请求中添加HTTP Header,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3541690/

相关文章:

php - (PHP + JQueryTools) Javascript 填充的输入字段在 POST 后返回 null

php - file_put_contents , 无法打开流 : No such file or directory Laravel

java - 使用 Jsoup 解析 SERPS 中的搜索描述(谷歌结果)

php - curl 发送 Content-length header 的问题

soap - Paypal 自适应付款响应非常慢

java - Postman 在 SOAP 响应中返回空值

php - 动态更改每行数据的下拉列表的选定状态

javascript - WordPress 中的 Qtip2/同位素冲突 : "Uncaught TypeError: Cannot call method ' call' of undefined"

java - HTTP请求返回非法状态异常android

java - Spring启动测试@WebMethod