Fatih
Fatih

Reputation: 235

Php curl get object data content

I created a function name is getURLContent. Curl return a json data example of data is:

{"request":{"method":"GET","version":"v1","product":"cloud","route":"project","func":"checkCJIsBusy"},"response":[{"id":2000858,"status":3},{"id":2000859,"status":7}]}

The data is decoded. The returned data is a stdclass object. I want to print values of id. How can I do?

function getURLContent($url, $method = "GET", $params = NULL, $timeout = 60) {
    if (strtoupper($method) == "GET" && $params) $url .= "?" . $params;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    if (strtoupper($method) == "POST") {
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params != NULL ? $params : "");
    }
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/2.0.0.9");
    curl_setopt($ch, CURLOPT_FAILONERROR, true);
    //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $data = curl_exec($ch);
    $chError = curl_error($ch);
    curl_close($ch);
    return json_decode($data);
}

$result=getURLContent($url);
print_r($result);

The result is print_r

stdClass Object ( [request] => stdClass Object ( [method] => GET [version] => v1 [product] => dwgcloud [route] => project [func] => checkCJIsBusy ) [response] => Array ( [0] => stdClass Object ( [id] => 2000858 [status] => 3 ) [1] => stdClass Object ( [id] => 2000859 [status] => 7 ) [2] => stdClass Object ( [id] => 2000860 [status] => 7 ) ) ) 

Upvotes: 1

Views: 2615

Answers (2)

Yaman Jain
Yaman Jain

Reputation: 1247

this method converts decoded json into associative array.

<?php
$json_string = '{"request":{"method":"GET","version":"v1","product":"cloud","route":"project","func":"checkCJIsBusy"},"response":[{"id":2000858,"status":3},{"id":2000859,"status":7}]}';

$result = json_decode($json_string,true);

foreach ($result["response"] as $value) {
    print_r($value["id"]);
    print_r("\n");
}

Upvotes: 1

Vitaliy Ryaboy
Vitaliy Ryaboy

Reputation: 478

try this code:

foreach ($result->response as $value){
    echo $value->id;
    echo "<br>";
}

Upvotes: 1

Related Questions