0

Hi I'm a little new at CURL, but I'm trying to request some json data and then parse the results. I am having success with retrieving the data, but I can't handle the response. Here's the code

function bitBucketCurl($url)
{
global $bitPassword;
global $bitUsername;

$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "$bitUsername:$bitPassword");
curl_setopt($ch, CURLOPT_HEADER, 0);

// grab URL and pass it to the browser
$commitinfo = curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);

return $commitinfo;
 }

$json = bitBucketCurl($url); 
echo $json; // This seems to work in that, when I load the page, I can see the json data

//turn json data into an array - this is what does not seem to be working
$obj_a = json_decode($json, true);

print_r ($obj_a); //the result is simply a 1 rather than the array I would expect

The basic problem is the json data shows up when I echo $json but when I try to turn that data into an array it doesn't work. When I print the array, I just get a '1'.

3
  • well it seems that adding this line curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); But if you can explain what this does and why it matters, I will happily award you answer credit :) Commented Feb 9, 2013 at 12:33
  • 2
    can you show us $json data? Commented Feb 9, 2013 at 12:36
  • json_encode's second parameter accepts constant values, and true is hardly what it does expect. Check the constants available at the docs. Commented Feb 10, 2013 at 11:14

1 Answer 1

1

I got the required result by adding the following line:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.