Extract JSON from cURL Results

I am new to regex and am trying to get a JSON response from a cURL request in PHP.

I thought to use preg_match_all.

Edit: Should have mentioned the full answer from curl_exec (), including the header information, so I need to extract the JSON.

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

JSON I want to look something like this (after all the headers):

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

I just wanted to take everything between curly braces. However, when I do this, it captures everything from the first open parenthesis to the first trailing curly bracket. For extensibility, I just want to capture everything inside and including the first open curly brace and the closing last bracket.

Technically, this can only begin with the first open bracket and return everything to the end of the response (nothing follows JSON there).

Thoughts?

+4
source share
2 answers

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

There is a json_decode() function 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 feature, you'd better write or use an existing JSON parser than try to extract parts using a regular expression.

If you use headers and you 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);` 
+12
source

as it helped me get closer to the result, but only “close” wanted 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" } 
+2
source

All Articles