Crhistian Ramirez Crhistian Ramirez - 3 months ago 14
PHP Question

How can I capture the response body from a curl GET?

My $output prints true, indicating the request was successful. How would I access the actual response body?

add_action( 'init', 'get_login_details');
function get_login_details(){
$curl = curl_init();
$auth_token = 'somerandomtoken';

curl_setopt($curl, CURLOPT_URL, "https://api.ordercloud.io/v1/me");
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Authorization: Bearer $auth_token'));

$output = curl_exec($curl);
print_r($output);
curl_close($curl);
}

Answer

Add curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1) and your body will be saved to $output.

Comments