Jeff
Jeff

Reputation: 4003

using json data from Curl response

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'.

Upvotes: 0

Views: 3600

Answers (1)

Jeff
Jeff

Reputation: 4003

I got the required result by adding the following line:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

Upvotes: 1

Related Questions