curl - php json post 印出?(使用URL)

查看:111
本文介绍了curl - php json post 印出?(使用URL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

假設有網址印出

{"message":"123。"}

我該如何利用 php 印出這段123文字?

$request = file_get_contents('php://input');
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://xxx.xxx.com/xxx/list");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json; charset=utf-8"));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
    $data = curl_exec($ch);
    curl_close($ch);

    echo $data['message'];

我這樣寫卻沒有東西?

解决方案

看不太懂,现在是https://xxx.xxx.com/xxx/list这个网址,打开浏览器输出了{"message":"123。"}这段内容?然后你要获取123?

$res = file_get_contents('https://xxx.xxx.com/xxx/list');
$res = json_decode($res, true);
echo $res['message'];

这篇关于curl - php json post 印出?(使用URL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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