搜索

首页  >  问答  >  正文

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

我这样写却没有东西?

为情所困为情所困2835 天前818

全部回复(2)我来回复

  • 我想大声告诉你

    我想大声告诉你2017-06-15 09:24:39

    看不太懂,现在是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
  • PHP中文网

    PHP中文网2017-06-15 09:24:39

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

    回复
    0
  • 取消回复