PHP:如何使用 XML 数据发送 REST 请求

发布于 2024-09-08 05:45:53 字数 104 浏览 2 评论 0原文

我需要在 PHP 中使用 REST(POST 方法)向 API 发出请求。

但数据需要采用 XML 格式。如何发送带有 XML 数据的 REST 请求?

谢谢你!

I need to make a request to an API, using REST (POST method) in PHP.

But the data needs to be in XML format. How can I send REST requests with XML data?

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悲念泪 2024-09-15 05:45:53

我使用“fopen”并且它有效。

//You can use 'POST','GET','PUT' or 'DELETE'
$opts = array(
    'http'=>array(
        'method'=>'POST',
        'header'=>"Content-Type: text/xml\r\n" .
            $auth."\r\n",
        'content'=>$xml
    )
);

$context = stream_context_create($opts);

/* Sends an http request to www.example.com
with additional headers shown above */
$fp = fopen($url, 'r', false, $context);
fpassthru($fp);
fclose($fp);

I used "fopen" and it works.

//You can use 'POST','GET','PUT' or 'DELETE'
$opts = array(
    'http'=>array(
        'method'=>'POST',
        'header'=>"Content-Type: text/xml\r\n" .
            $auth."\r\n",
        'content'=>$xml
    )
);

$context = stream_context_create($opts);

/* Sends an http request to www.example.com
with additional headers shown above */
$fp = fopen($url, 'r', false, $context);
fpassthru($fp);
fclose($fp);
倾城°AllureLove 2024-09-15 05:45:53

curl

这可用于精细设置多个标头 - POSTPUTDELETE - 用于您的 REST 请求以及发送有效负载 - 您的 XML 内容。

curl

This can be used to finely set several headers - POST, PUT, DELETE - for you REST request as well as send a payload - your XML content.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文