gpt4 book ai didi

php - 使用 curl 来使用此 Web 服务

转载 作者:行者123 更新时间:2023-12-02 07:09:39 25 4
gpt4 key购买 nike

我看到这篇关于使用 CURL 使用网络服务的帖子:Consume WebService with php

我一直在尝试遵循它,但运气不佳。我上传了我尝试访问的网络服务的照片。假设 URL 为以下示例,我将如何制定我的请求:

https://site.com/Spark/SparkService.asmx?op=InsertConsumer

enter image description here

我试过了,但它只返回一个空白页:

 $url = 'https://xxx.com/Spark/SparkService.asmx?op=InsertConsumer?NameFirst=Joe&NameLast=Schmoe&PostalCode=55555&EmailAddress=joe@schmoe.com&SurveyQuestionId=76&SurveyQuestionResponseId=1139';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);

$xmlobj = simplexml_load_string($result);
print_r($xmlobj);

最佳答案

真的,你应该看看 SOAP extension .如果它不可用或出于某种原因您必须使用 cURL,这里是一个基本框架:

<?php

// The URL to POST to
$url = "http://www.mysoapservice.com/";

// The value for the SOAPAction: header
$action = "My.Soap.Action";

// Get the SOAP data into a string, I am using HEREDOC syntax
// but how you do this is irrelevant, the point is just get the
// body of the request into a string
$mySOAP = <<<EOD
<?xml version="1.0" encoding="utf-8" ?>
<soap:Envelope>
<!-- SOAP goes here, irrelevant so wont bother writing it out -->
</soap:Envelope>
EOD;

// The HTTP headers for the request (based on image above)
$headers = array(
'Content-Type: text/xml; charset=utf-8',
'Content-Length: '.strlen($mySOAP),
'SOAPAction: '.$action
);

// Build the cURL session
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $mySOAP);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

// Send the request and check the response
if (($result = curl_exec($ch)) === FALSE) {
die('cURL error: '.curl_error($ch)."<br />\n");
} else {
echo "Success!<br />\n";
}
curl_close($ch);

// Handle the response from a successful request
$xmlobj = simplexml_load_string($result);
var_dump($xmlobj);

?>

关于php - 使用 curl 来使用此 Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7337221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com