使用PHP Curl发布数据并检索响应? [英] Post data and retrieve the response using PHP Curl?

查看:166
本文介绍了使用PHP Curl发布数据并检索响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用网络服务的新手,所以我发现这很混乱。

I'm very new to working with web services, and so I'm finding this pretty confusing.

如果我有一个网址,我试图发布一些JSON数据,我理解如何使用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?

谢谢!

-Elliot

推荐答案

您必须将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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆