dtdfj08626
dtdfj08626
采纳率0%
2017-08-08 12:48

使用PHP从JSON响应中提取值

已采纳

I'am sending JSON data to an URL and then I also recieve JSON.
I'm using PHP CURL to recieve the data and show it on my page.
My Problem is that I can only get the json completely and I can't show only a selected value from that JSON data.
I got this already:

//API Url
$url = "https://auth.unilinxx.com/test";

//Initiate cURL.
$ch = curl_init($url);

//The JSON data.
$jsonData = array("value" => "Gideon lol");

//Encode the array into JSON.
$jsonDataEncoded = json_encode($jsonData);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);

//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);

//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json")); 

//Execute the request
$result = curl_exec($ch);

$values = json_decode($result, true);

echo $values["value"];

My result is: {"value":"U heeft Gideon lol gestuurd."}

I like to have only this: U heeft Gideon lol gestuurd.

How can I do that?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • drtohng5613 drtohng5613 4年前

    Your entire code is correct you only have to add CURLOPT_RETURNTRANSFER to true in your code. Other wise result will not store in variable. And output to screen directly

    <?php
    //API Url
    $url = "https://auth.unilinxx.com/test";
    
    //Initiate cURL.
    $ch = curl_init($url);
    
    //The JSON data.
    $jsonData = array("value" => "Gideon lol");
    
    //Encode the array into JSON.
    $jsonDataEncoded = json_encode($jsonData);
    
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    //Tell cURL that we want to send a POST request.
    curl_setopt($ch, CURLOPT_POST, 1);
    
    //Attach our encoded JSON string to the POST fields.
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
    
    //Set the content type to application/json
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json")); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //<---------- Add this line
    //Execute the request
    $result = curl_exec($ch);
    
    $values = json_decode($result, true);
    
    echo $values["value"];
    
    点赞 评论 复制链接分享
  • douken0530 douken0530 4年前

    is your result variable return string? if it just return TRUE or FALSE please try to add this code

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
    点赞 评论 复制链接分享
  • dqt83336 dqt83336 3年前
    <?php
    //API Url
    $url = "https://auth.unilinxx.com/test";
    
    //Initiate cURL.
    $ch = curl_init($url);
    
    //The JSON data.
    $jsonData = array("value" => "Gideon lol");
    
    //Encode the array into JSON.
    $jsonDataEncoded = json_encode($jsonData);
    
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    //Tell cURL that we want to send a POST request.
    curl_setopt($ch, CURLOPT_POST, 1);
    
    //Attach our encoded JSON string to the POST fields.
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
    
    //Set the content type to application/json
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json")); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //<----- I will add this line
    //Execute the request
    $result = curl_exec($ch);
    
    $values = json_decode($result, true);
    
    点赞 评论 复制链接分享

相关推荐