使用 PHP Curl 发布数据并检索响应? [英] Post data and retrieve the response using PHP Curl?
问题描述
我对使用网络服务很陌生,所以我发现这很令人困惑.
I'm very new to working with web services, and so I'm finding this pretty confusing.
如果我有一个要向其发布一些 JSON 数据的 URL,我了解如何使用 CURL PHP 方法执行此操作.
If I have a URL I'm trying to post some JSON data to, I understand how to do this using the CURL PHP method.
我想知道的是,如果我这样做,并且 URL 有某种服务器响应..我如何在我的 php 中获得该响应并使用它在 PHP 中相应地采取不同的操作?
What I'm wondering is, if I do this, and the URL has some kind of server response.. how do I get that response in my php and use it to take different actions within the PHP accordingly?
谢谢!
-艾略特
推荐答案
您必须将 CURLOPT_RETURNTRANSFER 选项设置为 true.
You'll have to set the CURLOPT_RETURNTRANSFER option to true.
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
对您的请求的响应将在 $result 变量中可用.
The response to your request will be available in the $result variable.
这篇关于使用 PHP Curl 发布数据并检索响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!