Skip to content

Instantly share code, notes, and snippets.

@ubergeekzone
Created February 14, 2016 21:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ubergeekzone/972c798906933711137a to your computer and use it in GitHub Desktop.
Save ubergeekzone/972c798906933711137a to your computer and use it in GitHub Desktop.
Comparison between Requests for PHP and CURL
<?php
// Requests for PHP Code
$endpoint = "https://{$data['account']}.harvestapp.com/invoices";
$hashAuth = base64_encode($data['username'].":".$data['password']);
$response = Requests::get($endpoint, array('Content-Type' => 'application/json',
'accept' => 'application/json',
'authorization' => "Basic ". $hashAuth));
return $response->body;
//Non-Requests for PHP Code
$hashAuth = base64_encode($info['username'].":".$info['password']);
$today_date = date("Y-m-d");
$today_time = date("H:i:s");
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://{$data['account']}.harvestapp.com/invoices",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"accept: application/json",
"authorization: Basic ".$hashAuth,
"cache-control: no-cache",
"content-type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
if(!$err) {
return $response;
}
curl_close($curl);
?>
@rmccue
Copy link

rmccue commented Feb 15, 2016

FYI, you can pass in Basic auth and let Requests handle it for you:

$response = Requests::get(
    $endpoint,
    array('Content-Type' => 'application/json', 'accept' => 'application/json'),
    array('auth' => array($data['username'], $data['password'])
);

@ubergeekzone
Copy link
Author

@rmccue thanks that worked great and code looks cleaner and feels more smooth now. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment