Skip to content

Commit

Permalink
adds an HTTP Header User-Agent
Browse files Browse the repository at this point in the history
  • Loading branch information
tsamaya committed Mar 11, 2024
1 parent 0c7b72b commit 11c3633
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Set up PHP ${{ matrix.php-version }}
uses: shivammathur/setup-php@v2
Expand Down
5 changes: 4 additions & 1 deletion CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
3.3.0 -- Mon Mar 11 2024
=====================================================
- add an HTTP header: User-Agent

3.2.2 -- Mon Feb 26 2024
=====================================================
- add setProxy method
Expand Down Expand Up @@ -33,7 +37,6 @@
returned. file_get_contents is only used when the curl fetching doesn't
work (rare)


2.0.3 -- Sat Apr 27 2019
=====================================================
- API returns HTTP 401 for invalid key
Expand Down
38 changes: 38 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Using docker
#
# ```bash
# docker build -t local/opencage-php .
#
# docker run -ti -v `pwd`:/usr/src/myapp local/opencage-php
#
# app@ee88ad785ca2:/usr/src/myapp$ OPENCAGE_API_KEY=YOUR-OPENCAGE-API-KEY
# app@ee88ad785ca2:/usr/src/myapp$ composer install
# app@ee88ad785ca2:/usr/src/myapp$ ./vendor/bin/phpunit
# app@ee88ad785ca2:/usr/src/myapp$ SKIP_CURL=1 ./vendor/bin/phpunit
# app@ee88ad785ca2:/usr/src/myapp$ ./vendor/bin/phpcs .
# app@ee88ad785ca2:/usr/src/myapp$ ./vendor/bin/phpstan analyse --level 5 src tests demo
# ```

FROM ubuntu:22.04

ENV TZ=Europe/London

RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

RUN apt-get update -qq && \
apt-get install -y -qq curl vim php-cli php-curl php-xml php-mbstring unzip

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

RUN useradd --user-group --system --create-home --no-log-init app

COPY . /usr/src/myapp
WORKDIR /usr/src/myapp

RUN chown -R app:app /usr/src/myapp

USER app

VOLUME [ "/usr/src/myapp" ]

CMD ["/bin/bash"]
11 changes: 11 additions & 0 deletions src/AbstractGeocoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

abstract class AbstractGeocoder
{
const VERSION = '3.3.0'; // if changing this => remember to match everything with the git tag

const TIMEOUT = 10;
const URL = 'https://api.opencagedata.com/geocode/v1/json/?';
const PROXY = null;
Expand All @@ -12,13 +14,16 @@ abstract class AbstractGeocoder
protected $timeout;
protected $url;
protected $proxy;
protected $user_agent;

public function __construct($key = null)
{
if (isset($key) && !empty($key)) {
$this->setKey($key);
}
$this->setTimeout(self::TIMEOUT);
$this->user_agent =
'User-Agent: opencage-php/' . self::TIMEOUT . ' (PHP ' . phpversion() . '; ' . php_uname('s') . ' ' . php_uname('r') . ')';
}

public function setKey($key)
Expand Down Expand Up @@ -115,6 +120,12 @@ protected function getJSONByCurl($query)
}
curl_setopt_array($ch, $options);

$headers = [
$this->user_agent
];

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$ret = curl_exec($ch);
if ($ret === false) {
return $this->generateErrorJSON(498, 'network issue '.curl_error($ch));
Expand Down

0 comments on commit 11c3633

Please sign in to comment.