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'];

我這樣寫卻沒有東西?

阅读 3.7k
3 个回答

看不太懂,现在是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请求(get|post)到的结果是什么?如果是字符串(你给出的那个)那你直接json_decode就行,如果什么都没有,是不是你请求方式错了,网页打开有显示那应该是get吧

浏览器返回的是Json,直接json_decode

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题