Skip to content

Instantly share code, notes, and snippets.

@crazytonyi
Last active April 19, 2018 19:01
Show Gist options
  • Save crazytonyi/10222135ab7b4d9407d2103130a94cfa to your computer and use it in GitHub Desktop.
Save crazytonyi/10222135ab7b4d9407d2103130a94cfa to your computer and use it in GitHub Desktop.
<?php
$client = new \GuzzleHttp\Client();
$headers = ['x-api-key' => 'MY API KEY'];
$options = [
'headers' => ['x-api-key' => 'MY API KEY'],
'debug' => true,
];
try {
$resp = $client->request('GET', 'https://beta.check-mot.service.gov.uk',$options);
} catch (\GuzzleHttp\Exception\ClientException $e) {
$resp = $e->getResponse();
}
echo $resp->getBody();
@crazytonyi
Copy link
Author

Or, you could alias the Request class like use GuzzleHttp\Psr7\Request as GuzzleRequest; and then use $request = new GuzzleRequest to avoid the conflict with the Symfony Request class.

@ZalumsArtis
Copy link

When I make changes I get Class 'App\Http\Controllers\GuzzleHttp\Client' not found apparently this line causes it $client = new GuzzleHttp\Client();

@crazytonyi
Copy link
Author

Sorry. Fully qualified namespace should have the root slash in there to avoid that. Updated. give it one more go.

@ZalumsArtis
Copy link

Im sorry as well should of tried adding them. Also page came back with no errors besides {"message":"Missing Authentication Token"} but I have added the key and its correct as it works within command line

@crazytonyi
Copy link
Author

So working?

@ZalumsArtis
Copy link

In theory yes but Im getting the message that the Authentication Token is missing when its not? What could be the issue?

@ZalumsArtis
Copy link

From reading the document with the API theres 3 commands I can run. these are

  1. curl -H "Accept: application/json" -H "x-api-key: your_api_key " \https://beta.check-mot.service.gov.uk/trade/vehicles/mot-tests\?registration=XX10ABC
    To request the MOT Test History for registration XX10ABC

  2. curl -H "Accept: application/json" -H "x-api-key: your_api_key" \https://beta.check-mot.service.gov.uk/trade/vehicles/mot-tests\?page=[1-59310] (as of 04th Jan 2018)
    To request a full extract of the database. Please note that the last page normally increments by 10 each day.

  3. curl -H "Accept: application/json" -H "x-api-key: your_api_key" \https://beta.check-mot.service.gov.uk/trade/vehicles/mot-tests\?date=20170310\&page=[1-1440]
    To request MOT Test completed on the 10-03-2017 from page 1 to 1440

@ZalumsArtis
Copy link

Could that affect it in any way?

@crazytonyi
Copy link
Author

Try with the updated code. It has debugging set to true. Run the script from the command line, like php GuzzleTest6.php, and it will output the actual request and response before outputting the response body. Then compare that to:

curl -H "Accept: application/json" -H "x-api-key: your_api_key" 'https://beta.check-mot.service.gov.uk' -v

notice that I've added the -v to the command line curl request so it will output the full request and response headers. How are they different (the one from php GuzzleTest6.php versus the command line curl)?

@ZalumsArtis
Copy link

Command line output

HTTP/1.1 403 Forbidden
< Content-Type: application/json
< Content-Length: 43
< Connection: keep-alive
< Date: Thu, 19 Apr 2018 02:14:48 GMT
< x-amzn-RequestId: 6f6e2a1d-4377-11e8-aefe-210b6da53c7f
< x-amzn-ErrorType: MissingAuthenticationTokenException
< x-amz-apigw-id: FkQ_0E9SDoEF1sw=
< X-Cache: Error from cloudfront
< Via: 1.1 f188ead5b3846af391f02eedab2df3f7.cloudfront.net (CloudFront)
< X-Amz-Cf-Id: Q9LHWpigLg7mt6nAFg7WgsJsAQ7floQMLHKzETR0uLOVJmMi32UQNw==
< Set-Cookie: visid_incap_1151098=Ick2Zks/S9K9lXlWOyi040z711oAAAAAQUIPAAAAAACH8UNime+zC6lQXInR0m49; expires=Thu, 18 Apr 2019 12:14:28 GMT; path=/; Domain=.check-mot.service.gov.uk
< Set-Cookie: nlbi_1151098=5KbyFPMgYVh5xe6hR22hbQAAAABWyngYfdvolzaLUn1mE4wt; path=/; Domain=.check-mot.service.gov.uk
< Set-Cookie: incap_ses_374_1151098=W69QaliXiTs/QyL/N7cwBZf711oAAAAAmRMu9ruzH/iyYF6eAwfcTQ==; path=/; Domain=.check-mot.service.gov.uk
< Set-Cookie: ___utmvmSNukcFcB=QQQfcljpBiH; path=/; Max-Age=900
< Set-Cookie: ___utmvaSNukcFcB=sAf�uMdh; path=/; Max-Age=900
< Set-Cookie: ___utmvbSNukcFcB=gZV
< XCKOkald: Qtw; path=/; Max-Age=900
< X-Iinfo: 12-203895706-203895734 NNNN CT(0 2 0) RT(1524104087789 118) q(0 0 0 0) r(0 0) U5
< X-CDN: Incapsula
<
{"message":"Missing Authentication Token"}

  • Connection #0 to host wwwproxy.hud.ac.uk left intact

Website Output

  • Rebuilt URL to: https://beta.check-mot.service.gov.uk/ * Hostname wwwproxy.hud.ac.uk was found in DNS cache * Trying 161.112.232.111... * Connected to wwwproxy.hud.ac.uk (161.112.232.111) port 3128 (#0) * Establish HTTP proxy tunnel to beta.check-mot.service.gov.uk:443 > CONNECT beta.check-mot.service.gov.uk:443 HTTP/1.1 Host: beta.check-mot.service.gov.uk:443 Proxy-Connection: Keep-Alive < HTTP/1.1 200 Connection established < * Proxy replied OK to CONNECT request * ALPN, offering http/1.1 * Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@strength * successfully set certificate verify locations: * CAfile: /etc/ssl/certs/ca-certificates.crt CApath: /etc/ssl/certs * NPN, negotiated HTTP1.1 * SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256 * ALPN, server did not agree to a protocol * Server certificate: * subject: C=US; ST=Delaware; L=Dover; O=Incapsula Inc; CN=incapsula.com * start date: Mar 28 07:33:51 2018 GMT * expire date: Oct 16 09:29:59 2018 GMT * subjectAltName: beta.check-mot.service.gov.uk matched * issuer: C=BE; O=GlobalSign nv-sa; CN=GlobalSign CloudSSL CA - SHA256 - G3 * SSL certificate verify ok. > GET / HTTP/1.1 Host: beta.check-mot.service.gov.uk User-Agent: GuzzleHttp/6.3.2 curl/7.47.0 PHP/7.0.22-0ubuntu0.16.04.1 x-api-key: API KEY SHOULD BE HERE THO < HTTP/1.1 403 Forbidden < Content-Type: application/json < Content-Length: 42 < Connection: keep-alive < Date: Thu, 19 Apr 2018 02:13:37 GMT < x-amzn-RequestId: 44f228c6-4377-11e8-9dfe-85e2ebc0c48b < x-amzn-ErrorType: MissingAuthenticationTokenException < x-amz-apigw-id: FkQ0rEdwjoEF4Kg= < X-Cache: Error from cloudfront < Via: 1.1 0c0143d8fd59491aa01de3c647d661d6.cloudfront.net (CloudFront) < X-Amz-Cf-Id: dKS1eV43JY2hn9ynNoyC32JjSZ3XNPIyPaZBjpL61F7_0xaEK0lGBQ== < Set-Cookie: visid_incap_1151098=Ick2Zks/S9K9lXlWOyi040z711oAAAAAQUIPAAAAAACH8UNime+zC6lQXInR0m49; expires=Thu, 18 Apr 2019 12:14:16 GMT; path=/; Domain=.check-mot.service.gov.uk < Set-Cookie: nlbi_1151098=xCiSI0vejEBc7z7hR22hbQAAAAC7TLHpJjxYogT/fDNT8fk2; path=/; Domain=.check-mot.service.gov.uk < Set-Cookie: incap_ses_374_1151098=LcNWf5hzaxo/QyL/N7cwBVD711oAAAAAOm8N78FOoV7x3AcUCCoJUA==; path=/; Domain=.check-mot.service.gov.uk < X-Iinfo: 14-317150952-317150964 NNNN CT(0 7 0) RT(1524104016581 43) q(0 0 0 0) r(0 0) U5 < X-CDN: Incapsula < * Connection #0 to host wwwproxy.hud.ac.uk left intact {"message":"Missing Authentication Token"}

@crazytonyi
Copy link
Author

That's using a valid API key for both?

@ZalumsArtis
Copy link

Oh no just noticed

@ZalumsArtis
Copy link

  • Rebuilt URL to: https://beta.check-mot.service.gov.uk/
  • Trying 161.112.232.221...
  • Connected to wwwproxy.hud.ac.uk (161.112.232.221) port 3128 (#0)
  • Establish HTTP proxy tunnel to beta.check-mot.service.gov.uk:443

CONNECT beta.check-mot.service.gov.uk:443 HTTP/1.1
Host: beta.check-mot.service.gov.uk:443
User-Agent: curl/7.47.0
Proxy-Connection: Keep-Alive

< HTTP/1.1 200 Connection established
<

  • Proxy replied OK to CONNECT request
  • found 173 certificates in /etc/ssl/certs/ca-certificates.crt
  • found 694 certificates in /etc/ssl/certs
  • ALPN, offering http/1.1
  • SSL connection using TLS1.2 / ECDHE_RSA_AES_128_GCM_SHA256
  • server certificate verification OK
  • server certificate status verification SKIPPED
  • common name: incapsula.com (matched)
  • server certificate expiration date OK
  • server certificate activation date OK
  • certificate public key: RSA
  • certificate version: #3
  • subject: C=US,ST=Delaware,L=Dover,O=Incapsula Inc,CN=incapsula.com
  • start date: Wed, 28 Mar 2018 07:33:51 GMT
  • expire date: Tue, 16 Oct 2018 09:29:59 GMT
  • issuer: C=BE,O=GlobalSign nv-sa,CN=GlobalSign CloudSSL CA - SHA256 - G3
  • compression: NULL
  • ALPN, server did not agree to a protocol

GET / HTTP/1.1
Host: beta.check-mot.service.gov.uk
User-Agent: curl/7.47.0
Accept: application/json
x-api-key: API KEY

< HTTP/1.1 403 Forbidden
< Content-Type: application/json
< Content-Length: 43
< Connection: keep-alive
< Date: Thu, 19 Apr 2018 02:23:32 GMT
< x-amzn-RequestId: a7b95f2a-4378-11e8-823e-1707b1755be6
< x-amzn-ErrorType: MissingAuthenticationTokenException
< x-amz-apigw-id: FkSRrEcqjoEFtEA=
< X-Cache: Error from cloudfront
< Via: 1.1 db29ce630f36ae7f48a6957d9d80d0be.cloudfront.net (CloudFront)
< X-Amz-Cf-Id: on1G43o2CYZFH2yRcsUP3oHYZQQhWgw76nPyf8Y2vV_zJOy1yeqh2g==
< Set-Cookie: visid_incap_1151098=X2wckoG7QmybXDPqnkMqq6P911oAAAAAQUIPAAAAAADGm6ee6C6EhpemMK9YwpYd; expires=Thu, 18 Apr 2019 12:14:28 GMT; path=/; Domain=.check-mot.service.gov.uk
< Set-Cookie: nlbi_1151098=OTNSeizpYUF7Qr0BR22hbQAAAABu3Md5TDawJ/lVqbRs89aW; path=/; Domain=.check-mot.service.gov.uk
< Set-Cookie: incap_ses_374_1151098=snDnCPTc/UoDDCP/N7cwBaP911oAAAAA/MtqNe7LCKrwvTTtEI8f3A==; path=/; Domain=.check-mot.service.gov.uk
< Set-Cookie: ___utmvmSNukcFcB=AAgXhOmvhYx; path=/; Max-Age=900
< Set-Cookie: ___utmvaSNukcFcB=XvV�lOYx; path=/; Max-Age=900
< Set-Cookie: ___utmvbSNukcFcB=oZn
< XjyOUalR: itK; path=/; Max-Age=900
< X-Iinfo: 6-48709975-48709978 NNNN CT(1 6 0) RT(1524104611734 108) q(0 0 0 0) r(0 0) U5
< X-CDN: Incapsula
<
{"message":"Missing Authentication Token"}

  • Connection #0 to host wwwproxy.hud.ac.uk left intact

@ZalumsArtis
Copy link

This is the output

@crazytonyi
Copy link
Author

The output from what? That looks like it's all command line curl, is that right? If so, it seems like you get a 403 response no matter what. In which case it's not an issue with the code, right?

@ZalumsArtis
Copy link

Thats from command line yes. If I use curl -H "Accept: application/json" -H "x-api-key: your_api_key " \https://beta.check-mot.service.gov.uk/trade/vehicles/mot-tests\?registration=XX10ABC, I get a output and works with any car reg

@crazytonyi
Copy link
Author

Does that also work if you update the URL for the guzzle request?

@ZalumsArtis
Copy link

Would I need to update the actual url within controller?

@crazytonyi
Copy link
Author

The URL in the request.

@ZalumsArtis
Copy link

By using function?

public function apitest(){

$client = new \GuzzleHttp\Client();
$headers = ['x-api-key' => 'Key here'];

$options = [
'headers' => ['x-api-key' => 'Key here'],
'debug' => true,
];
try {
$resp = $client->request('GET', 'https://beta.check-mot.service.gov.uk/trade/vehicles/mot-tests\?registration=XX10ABC
',$options);
} catch (\GuzzleHttp\Exception\ClientException $e) {
$resp = $e->getResponse();
}
echo $resp->getBody();

}

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