From 11c363378afc87359e245eb955bd67da004dc262 Mon Sep 17 00:00:00 2001 From: Arnaud Ferrand Date: Mon, 11 Mar 2024 15:46:10 +0000 Subject: [PATCH] adds an HTTP Header User-Agent --- .github/workflows/ci.yml | 2 +- CHANGES.txt | 5 ++++- Dockerfile | 38 ++++++++++++++++++++++++++++++++++++++ src/AbstractGeocoder.php | 11 +++++++++++ 4 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 Dockerfile diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0a3e5d2..ab24cfb 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/CHANGES.txt b/CHANGES.txt index 09e2e57..a865179 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -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 @@ -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 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..7e8b750 --- /dev/null +++ b/Dockerfile @@ -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"] \ No newline at end of file diff --git a/src/AbstractGeocoder.php b/src/AbstractGeocoder.php index 77f30b3..5c3fd44 100644 --- a/src/AbstractGeocoder.php +++ b/src/AbstractGeocoder.php @@ -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; @@ -12,6 +14,7 @@ abstract class AbstractGeocoder protected $timeout; protected $url; protected $proxy; + protected $user_agent; public function __construct($key = null) { @@ -19,6 +22,8 @@ public function __construct($key = null) $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) @@ -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));