PHP Curl在浏览器上返回空字符串,在邮递员中工作

时间:2019-06-14 13:28:59

标签: php xml curl text postman

我有一些由Postman配置的Php Curl代码,我的API SOAP调用可以在Postman上完美运行,但是将其移入浏览器后,我得到一个空字符串

我已经测试过将TEXT / XML更改为JSON,因为我有一个类似的调用可以在JSON / APPLICATION中使用100%的语言,并且支持多种语言,但是似乎都返回了空字符串。

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://example.com/webservice.cfc?Wsdl=",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:hs=\"https://example.com/webservice.cfc?Wsdl\">\n    <soapenv:Body>\n        <hs:examplecall>\n            <hs:ws_username>xxx</hs:ws_username>\n            <hs:ws_password>xxx</hs:ws_password>\n            <hs:exampleparam>xxx</hs:exampleparam>\n        </hs:examplecall>\n    </soapenv:Body>\n</soapenv:Envelope>",
  CURLOPT_HTTPHEADER => array(
    "Accept: */*",
    "Cache-Control: no-cache",
    "Connection: keep-alive",
    "Content-Type: text/xml",
    "Postman-Token: xxx",
    "User-Agent: PostmanRuntime/7.13.0",
    "accept-encoding: gzip, deflate",
    "cache-control: no-cache",
    "content-length: 445",
    "cookie: CFID=xxx; CFTOKEN=xxx"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

邮递员返回的正是我想要的东西,但邮递员生成的上述代码返回的是一个没有错误的空字符串,所以我没什么可继续的。

2 个答案:

答案 0 :(得分:3)

这是SOAP调用。您可能要尝试这样的事情:

$client = new SoapClient('https://example.com/webservice.cfc?Wsdl');

$response = $client->examplecall(
        [
            'ws_username' => 'some_username',
            'ws_password' => 'some_pass',
            'exampleparam' => 'some_param'
        ]
);

print_r($response);

答案 1 :(得分:0)

让它工作并返回我想要的方式-问题是XML的格式与WSDL文件不一致。我不知道为什么,但是邮递员还在WSDL的末尾添加了一个=符号,并且XML的格式不同-这是用于使其工作的代码:

<?php

$xml_data = '
  <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:hs="https://example.com/webservice.cfc?Wsdl">
    <soapenv:Body>
        <hs:exampleCall>
            <hs:ws_username>UN</hs:ws_username>
            <hs:ws_password>PW</hs:ws_password>
            <hs:exampleParam>xxx</hs:exampleParam>
        </hs:exampleCall>
    </soapenv:Body>
</soapenv:Envelope>
';

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://example.com/webservice.cfc?Wsdl",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => $xml_data,
  CURLOPT_HTTPHEADER => array(
    "Content-Type: text/xml; charset=utf-8",
    "Postman-Token: xxx",
    "cache-control: no-cache",
    "SOAPAction: ;",
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  print_r($response);
}