如何用PHP和CURL发送JSON字符串的示例代码?
- 内容介绍
- 文章标签
- 相关推荐
本文共计480个文字,预计阅读时间需要2分钟。
php/** * 使用CURL发送JSON格式字符串的方法 * * @param $url string URL * @param $data_string string 请求的具体内容 * @return array */function sendJsonWithCurl($url, $data_string) { // 初始化CURL会话 $ch=curl_init();
// 设置CURL选项 curl_setopt($ch, CURLOPT_URL, $url); // 设置请求的URL curl_setopt($ch, CURLOPT_POST, true); // 设置为POST请求 curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); // 设置POST请求的内容 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 将结果以字符串形式返回 curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); // 设置请求头,指定内容类型为JSON
// 执行CURL会话 $result=curl_exec($ch);
// 关闭CURL会话 curl_close($ch);
// 返回结果 return json_decode($result, true);}
本文实例讲述了PHP基于CURL发送JSON格式字符串的方法。
本文共计480个文字,预计阅读时间需要2分钟。
php/** * 使用CURL发送JSON格式字符串的方法 * * @param $url string URL * @param $data_string string 请求的具体内容 * @return array */function sendJsonWithCurl($url, $data_string) { // 初始化CURL会话 $ch=curl_init();
// 设置CURL选项 curl_setopt($ch, CURLOPT_URL, $url); // 设置请求的URL curl_setopt($ch, CURLOPT_POST, true); // 设置为POST请求 curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); // 设置POST请求的内容 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 将结果以字符串形式返回 curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); // 设置请求头,指定内容类型为JSON
// 执行CURL会话 $result=curl_exec($ch);
// 关闭CURL会话 curl_close($ch);
// 返回结果 return json_decode($result, true);}
本文实例讲述了PHP基于CURL发送JSON格式字符串的方法。

