php - 无法在远程 API 服务器上接收通过 cURL 发送的入站 XML
问题描述
我的任务是构建一个 API 来接收入站 XML 数据。在我的客户上,我有这个代码。
$url = "http://stackoverflow.com";
$xml = '<?xml version="1.0" encoding="UTF-8"?><Request PartnerID="asasdsadsa" Type="TrackSearch"> <TrackSearch> <Title>love</Title> <Tags> <MainGenre>Blues</MainGenre> </Tags> <Page Number="1" Size="20"/> </TrackSearch> </Request>';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt( $ch, CURLOPT_POSTFIELDS, "xml=".$payload );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$request = curl_exec($ch);
curl_close($ch);
在我的远程服务器上,我有这个代码
function TransmitRx()
{
$xml = trim(file_get_contents('php://input'));
file_put_contents("newStandard/".rand(100,500)."received.xml", $xml);
}
//Listen for inbound data
TransmitRx()
如果我打开服务器端点 URL,则会保存一个空文件。我不知道为什么。但是当我运行客户端脚本时。我什么都得不到。没有错误。没有什么。
我已经查看了这里的几个页面,每个页面都有一个类似的 cURL 语句来发送数据。
为什么我没有在 API 端点收到任何发布数据?
我通过万维网获取的任何信息都没有成功。
更新
有效的最终代码:
function get_url($request_url, $payload)
{
$headers = [
"Access-Control-Allow-Origin: *",
"Content-type: text/xml",
"Content-length: " . strlen($payload),
"Connection: close",
];
$data = ['xml' => $payload];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$response = curl_exec($ch) or die(curl_error($ch));;
if (curl_errno($ch)) {
print curl_error($ch);
} else {
curl_close($ch);
}
return $response;
}
$request_url = 'https://stackoverflow.com/posts';
$response = get_url($request_url, $payload);
我希望我能确定是什么导致它开始工作。我上次正在阅读这一页。
解决方案
好的,所以如果添加我建议禁用认证/对等验证的行使该过程能够工作,那么这仅意味着远程服务器正在使用 cURL 不信任的 SSL 证书。
这些行不是最终解决方案。您永远不想在真实环境中禁用 SSL 验证。我只建议您暂时禁用它们,看看这是否真的是问题所在。如果您将它们禁用,那么您的代码就会容易受到中间人 (MITM) 攻击。
正确的解决方法通常是将 cURL 指向更新的 CA 包。官方 cURL 网站在这里慷慨地提供了这些:
https://curl.haxx.se/docs/caextract.html
该过程是您下载 CA 捆绑文件并将其放在脚本可以找到的位置,然后添加此 curl 选项:
curl_setopt ($ch, CURLOPT_CAINFO, "full path to the CA bundle file.pem");
如果远程服务器的证书来自任何主要的 CA 供应商,那么这就是您需要做的所有事情。
如果远程服务器的证书是自签名的或其他什么,那么您可能需要下载特定的 CA 证书和任何支持的中间 CA 证书并告诉 cURL 找到它们。
推荐阅读
- arrays - Create array of empty subarrays in Ruby
- ios - Ask Permissions before use in-built features in iOS
- java - gradle.properies 中的 Gradle 系统属性没有被使用吗?(javacpp,智能)
- raspberry-pi3 - Raspberry Pi 无法在某些 HDMI 显示器上工作
- mysql - MySQL - how to show some field in select query that accept my condition
- python - 通过命令更改复合审核日志
- java - 如何解决 Selenium 中的 org.openqa.selenium.NoSuchElementException?
- python - 为什么我的 Django 表单无效?
- laravel - Validator::validateNumber 不存在
- arrays - 调整向量大小 Julia