首页 >社区问答列表 >curl - php json post 印出?(使用URL)

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

我這樣寫卻沒有東西?

  • 我想大声告诉你
  • 我想大声告诉你    2017-06-15 09:24:392楼

    看不太懂,现在是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'];

    +0添加回复

  • 回复
  • 小皮
  • 小皮    2017-06-15 09:24:391楼

    curl请求(get|post)到的结果是什么?如果是字符串(你给出的那个)那你直接json_decode就行,如果什么都没有,是不是你请求方式错了,网页打开有显示那应该是get吧

    +0添加回复

  • 回复