curl - php json post print out? (Use URL)
为情所困
为情所困 2017-06-15 09:22:39
0
2
766

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?

为情所困
为情所困

reply all(2)
我想大声告诉你

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

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template