Skip to content

Commit

Permalink
fix merge bewteen default and non default curl headers
Browse files Browse the repository at this point in the history
  • Loading branch information
maxiloc committed Jun 28, 2016
1 parent 8781bad commit 211b36e
Showing 1 changed file with 24 additions and 17 deletions.
41 changes: 24 additions & 17 deletions src/AlgoliaSearch/Client.php
Expand Up @@ -804,27 +804,34 @@ public function doRequest(
}

//curl_setopt($curlHandle, CURLOPT_VERBOSE, true);
$curlHeaders = array();
foreach ($context->headers as $key => $value) {
$curlHeaders[] = $key.': '.$value;
}

$defaultHeaders = null;
if ($context->adminAPIKey == null) {
curl_setopt($curlHandle, CURLOPT_HTTPHEADER, array_merge(array(
'X-Algolia-Application-Id: '.$context->applicationID,
'X-Algolia-API-Key: '.$context->apiKey,
'Content-type: application/json',
), $curlHeaders));
$defaultHeaders = array(
'X-Algolia-Application-Id' => $context->applicationID,
'X-Algolia-API-Key' => $context->apiKey,
'Content-type' => 'application/json',
);
} else {
curl_setopt($curlHandle, CURLOPT_HTTPHEADER, array_merge(array(
'X-Algolia-Application-Id: '.$context->applicationID,
'X-Algolia-API-Key: '.$context->adminAPIKey,
'X-Forwarded-For: '.$context->endUserIP,
'X-Algolia-UserToken: '.$context->algoliaUserToken,
'X-Forwarded-API-Key: '.$context->rateLimitAPIKey,
'Content-type: application/json',
), $curlHeaders));
$defaultHeaders = array(
'X-Algolia-Application-Id' => $context->applicationID,
'X-Algolia-API-Key' => $context->adminAPIKey,
'X-Forwarded-For' => $context->endUserIP,
'X-Algolia-UserToken' => $context->algoliaUserToken,
'X-Forwarded-API-Key' => $context->rateLimitAPIKey,
'Content-type' => 'application/json',
);
}

$headers = array_merge($defaultHeaders, $context->headers);

$curlHeaders = array();
foreach ($headers as $key => $value) {
$curlHeaders[] = $key.': '.$value;
}

curl_setopt($curlHandle, CURLOPT_HTTPHEADER, $curlHeaders);

curl_setopt($curlHandle, CURLOPT_USERAGENT, 'Algolia for PHP '.Version::get());
//Return the output instead of printing it
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
Expand Down

0 comments on commit 211b36e

Please sign in to comment.