2

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?

8
  • php.net/manual/en/function.json-decode.php Commented Aug 8, 2017 at 12:49
  • I already used that as you can see in my code! Commented Aug 8, 2017 at 12:50
  • Are you viewing the JSON string "auth.unilinxx.com/test" is outputting to help you see what's going on here? It looks like your API is returning a nested JSON array of "value"{"value":{"U heeft... Commented Aug 8, 2017 at 12:52
  • 1
    unless you have json inside json, this is not a correct result Commented Aug 8, 2017 at 12:53
  • echo $values["value"]; would fail if your output was {"value":"U heeft Gideon lol gestuurd."} so something tells me this code is not what is being ran or $values["value"] is a string of that data structure... Commented Aug 8, 2017 at 12:53

3 Answers 3

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"];
Sign up to request clarification or add additional context in comments.

1 Comment

I am doing the same thing with ajax request, when I just echo the $results it gives the json string but when I add $values = json_decode($result, true); jquery console.log returns null why is that?
2

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);

Comments

0
<?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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.