搜尋

首頁  >  問答  >  主體

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

我這樣寫卻沒有東西?

为情所困为情所困2795 天前798

全部回覆(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
  • 取消回覆