简体   繁体   English

无法在远程 API 服务器上接收通过 cURL 发送的入站 XML

[英]Unable to receive inbound XML sent via cURL on remote API server

I am tasked with building an API to receive inbound XML data.我的任务是构建一个 API 来接收入站 XML 数据。 On my client, I have this code.在我的客户端上,我有这个代码。

 $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);

On my remote server, I have this code在我的远程服务器上,我有这个代码

 function TransmitRx()
 {
    $xml = trim(file_get_contents('php://input'));
    file_put_contents("newStandard/".rand(100,500)."received.xml", $xml);
 }

 //Listen for inbound data 
 TransmitRx()

If I open up the server endpoint URL, there is an empty file saved.如果我打开服务器端点 URL,则会保存一个空文件。 I don't know why.我不知道为什么。 But when I run the client-side script.但是当我运行客户端脚本时。 I get nothing.我什么也得不到。 No errors.没有错误。 Nothing.没有。

I have looked at several of the pages here and every one of them has a similar cURL statement to send data.我看过这里的几个页面,每个页面都有一个类似的 cURL 语句来发送数据。

Why am I not receiving any post data at the API endpoint?为什么我在 API 端点没有收到任何发布数据?

I have been unsuccessful at any information via the WWW.我未能通过 WWW 获取任何信息。

UPDATE更新

Final Code that works:最终有效的代码:

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);

I wish I knew for sure what caused it to start working.我希望我确切地知道是什么导致它开始工作。 I was reading this page last.我上次阅读了这一页。

https://www.php.net/manual/en/curlfile.construct.php https://www.php.net/manual/en/curlfile.construct.php

Okay, so if adding the lines I suggested to disable certification / peer validation enabled the process to work, then that just means the remote server is using an SSL certificate that is not trusted by cURL.好的,所以如果添加我建议禁用认证/对等验证的行使该过程能够工作,那么这仅意味着远程服务器正在使用不受 cURL 信任的 SSL 证书。

Those lines are NOT the final fix.这些行不是最终解决方案。 You never want to disable SSL validation in a real environment.您永远不想在真实环境中禁用 SSL 验证。 I only suggested you temporarily disable them to see if that was truly the problem.我只建议你暂时禁用它们,看看这是否真的是问题所在。 If you leave them disabled, then you are leaving your code vulnerable to man-in-the-middle (MITM) attacks.如果您将它们禁用,那么您的代码就容易受到中间人 (MITM) 攻击。

The correct fix is usually to point cURL at an updated CA bundle.正确的修复通常是将 cURL 指向更新的 CA 包。 The official cURL website graciously provides these here:官方 cURL 网站慷慨地提供了这些:

https://curl.haxx.se/docs/caextract.html https://curl.haxx.se/docs/caextract.html

The process is that you download the CA bundle file and put it somewhere where your script can find it, and then add this curl option:过程是您下载 CA 包文件并将其放在脚本可以找到的地方,然后添加此 curl 选项:

curl_setopt ($ch, CURLOPT_CAINFO, "full path to the CA bundle file.pem");

If the remote server's certificate came from any of the major CA vendors out there, this should be all you need to do.如果远程服务器的证书来自任何主要的 CA 供应商,这应该是您需要做的全部。

If the remote server's certificate is self-signed or something then you might need to download the specific CA certificate and any supporting intermediate CA certificates and tell cURL to find them.如果远程服务器的证书是自签名的或其他什么,那么您可能需要下载特定的 CA 证书和任何支持的中间 CA 证书,并告诉 cURL 找到它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM