【问题标题】:Access web service via curl PHP or some thing similar通过 curl PHP 或类似的东西访问 Web 服务
【发布时间】:2018-02-01 10:32:17
【问题描述】:

我有这种类型的 API 代码,如何发送请求以在 php 中访问此 Web 服务?我可以使用卷曲吗?请帮帮我

沙盒网址: http://api.lankagate.gov.lk:8280/GetOnGoingVehicleNoDMT/1.0 方法 POST

参数名称 - 车辆类别 选项 - 1 , 4

成功响应代码:200

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <soap:Body>
 <GetOnGoingVehicleNoResponse
xmlns="http://schemas.conversesolutions.com/xsd/dmticta/v1">
 <return>
 <ResponseMessage xsi:nil="true" />
 <ErrorCode xsi:nil="true" />
 <OngoingVehicleNo>CAW-2186</OngoingVehicleNo>
 </return>
 </GetOnGoingVehicleNoResponse>
 </soap:Body>
</soap:Envelope>

Sample

Call URL : http://api.lankagate.gov.lk:8280/Transliteration/1.0/
Message body :
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:v1="http://schemas.conversesolutions.com/xsd/dmticta/v1">
 <soapenv:Header/>
 <soapenv:Body>
 <v1:GetOnGoingVehicleNo>
 <v1:vehicleCategory>1</v1:vehicleCategory>
 </v1:GetOnGoingVehicleNo>
 </soapenv:Body>
</soapenv:Envelope>

Notes 将请求标头设置为 授权:承载访问令牌

我试过了,这是我的代码

function get_exam_mode_of_study() {
        $xml_data = '<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:v1="http://schemas.conversesolutions.com/xsd/dmticta/v1">
 <soapenv:Header/>
 <soapenv:Body>
 <v1:GetOnGoingVehicleNo>
 <v1:vehicleCategory>1</v1:vehicleCategory>
 </v1:GetOnGoingVehicleNo>
 </soapenv:Body>
</soapenv:Envelope>';
        $URL = "http://api.lankagate.gov.lk:8280/GetOnGoingVehicleNoDMT/1.0";
        $ch = curl_init($URL);
//        curl_setopt($ch, CURLOPT_MUTE, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
        curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml_data");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        print_r($output);
        exit;
        curl_close($ch);
//        }
    }

它给出了以下错误

<ams:fault xmlns:ams="http://wso2.org/apimanager/security"><ams:code>900902</ams:code><ams:message>Missing Credentials</ams:message><ams:description>Required OAuth credentials not provided. Make sure your API invocation call has a header: "Authorization: Bearer ACCESS_TOKEN"</ams:description></ams:fault>

【问题讨论】:

  • “我可以使用 curl 吗?” - 你为什么不试一试呢?这个问题缺少任何代码来向我们表明您已尝试自己解决此问题。如果您尝试过某些东西,请向我们展示您尝试过的内容、预期输出的示例以及您实际得到的结果。如果您没有尝试过任何事情,则需要在发布之前这样做。我们可以为您的现有代码提供帮助,但我们不会为您编写。请阅读:How to create a Minimal, Complete, and Verifiable example 以及 [如何提出一个好问题?](stackoverflow.com/help/how-t
  • @MagnusEriksson 已更新
  • 就像消息说的那样,您没有添加 auth 标头。在您的 curl 通话中,尝试将 array('Content-Type: text/xml') 更改为 array('Content-Type: text/xml', 'Authorization: Bearer your-access-token-here') 以获取 CURLOPT_HTTPHEADER 选项。

标签: php web-services curl php-curl


【解决方案1】:

您需要添加授权标头

curl_setopt() 的 CURLOPT_HTTPHEADER 采用一个数组,其中每个标头作为一个元素。

你可以使用下面的 $header 数组。

$header = array();
$header[] = 'Content-length: 0';
$header[] = 'Content-type: application/json';
$header[] = 'Authorization: Bearer OAuthAccess_TokenThatIReceived';

curl_setopt($ch, CURLOPT_HTTPHEADER,$header);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-15
    相关资源
    最近更新 更多