Created
February 14, 2016 21:47
-
-
Save ubergeekzone/972c798906933711137a to your computer and use it in GitHub Desktop.
Comparison between Requests for PHP and CURL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
FYI, you can pass in Basic auth and let Requests handle it for you: