search

Home  >  Q&A  >  body text

curl - php json post print out? (Use URL)

Assume there is a URL printed out

{"message":"123。"}

How should I use php to print out this 123 text?

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

I write like this but there is nothing?

为情所困为情所困2835 days ago823

reply all(2)I'll reply

  • 我想大声告诉你

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

    I can’t quite understand it. Now it’s https://xxx.xxx.com/xxx/list. When I open the browser, it outputs {"message":"123."}This content? And then you want to get 123?

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

    reply
    0
  • PHP中文网

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

    What is the result of curl request (get|post)? If it is a string (the one you gave), then you can just json_decode it. If there is nothing, is your request method wrong? If the page is opened and it is displayed, it should be get.

    reply
    0
  • Cancelreply