Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting JSON from cURL results

I'm new to regex and I'm trying to grab the JSON response from a cURL request in PHP.

I was thinking of using preg_match_all.

Edit: Should have mentioned the full response from curl_exec() includes header information which is why I need to extract the JSON.

HTTP/1.1 401 Unauthorized Server: Apache-Coyote/1.1 WWW-Authenticate:  Digest realm="",  qop="auth", [... etc]

The JSON I want looks something like this (following all headers):

{  "requests" :  
  [ { 
     "request_id" : 10298, 
     "name" : "CURL Test2",  
     "submitter" : "First Last",  
     "hide" : false,  
     "priority" : 10,  
     "tags" : [ "label 2" ],  
     "body" : 
          { "type" : "html", "content" : "" },  
     "runs" : 0  
   } ] 
  }

Was hoping to just grab everything between the curly braces. However, when I do this it grabs everything from the first opening brace to the first closing brace. For extensibility, I just want to grab everything within and including the first opening brace and the last closing brace.

Technically it could just start at the first opening brace and return everything until end of the response (there's nothing following the JSON).

Thoughts?

like image 644
djreed Avatar asked Dec 07 '22 21:12

djreed


2 Answers

Regex is great, but definitely not the correct tool for this.

There is a function json_decode() that can handle this for you.

It will return the structure as an object. You can return it as an array by setting the second argument to TRUE. Even if PHP didn't have this function, you are better off writing or using an existing JSON parser than trying to extract portions with regex.

If you are using the headers and need to separate the body into a separate variable, you should do something like the following where $ch is an instance of curl and $result is the return of curl_exec().

$headerLen = curl_getinfo($ch, CURLINFO_HEADER_SIZE); 
$curlBody = substr($result, $headerLen);`
like image 66
alex Avatar answered Dec 09 '22 09:12

alex


as this helped me near to the result but only "near" want to share my result ..

$json = array ( "firstname" => "john" , "lastname" => "Doe") ; 
$jsonheader = array ( "Accept: application/json" 
, "Content-type:application/json" 
, "Authorization: OAuth oauth_token=xxxxx" ) ;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://myurl.com?oAuth=12341231234' ); // set the target url
curl_setopt($ch, CURLOPT_POST, 1 ); // howmany parameter to post
curl_setopt($ch, CURLOPT_POSTFIELDS, $json ); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true ); // don't give anything back 
curl_setopt($ch, CURLOPT_HEADER, TRUE );  // need this to evaluate the response .. 
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $jsonheader ); // it is httpheader not httpheader"s" 

$result = curl_exec ($ch);

curl_close ($ch);
$resultarr = explode ( "\n" , $result ) ;
$httpval = explode ( " " , $resultarr[0] ) ; // explode the first line

for ( $i=1 ;  $i < count( $resultarr) ; $i++ ) {
    if ( is_array (json_decode( $resultarr[$i] , true)) ){
        $resultvals  = json_decode( $resultarr[$i] , true) ;
    }       
}
if ( $resultvals['YourKey'] <> '' ) {  // any accepted value in the response ... 
    if ( $httpval[1] == "201" )  {
        $error = 0 ; //  no error Overwrite error 2 
    } else {
        $error = 1 ;  // http response was not 201  .. 
    }
} else {
   $error = 2 ;  // any other error like "no response" or "other error"   
 }
like image 29
velletti Avatar answered Dec 09 '22 11:12

velletti