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

问题描述 投票:0回答:1

我的任务是构建一个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端点上没有收到任何发布数据?

我无法通过WWW获得任何信息。

UPDATE

有效的最终代码:

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

我希望我确定是什么原因导致它开始工作。我上次正在阅读此页面。

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

php api curl
1个回答
0
投票

好,所以如果添加我建议禁用证书/对等验证的行使该过程能够正常工作,那仅意味着远程服务器正在使用cURL不信任的SSL证书。

© www.soinside.com 2019 - 2024. All rights reserved.