Skip to content

Commit a596a33

Browse files
authored
Merge pull request #94 from ConvertKit/v4-api-get-html-resource
v4 API: Get HTML Resources
2 parents 10f8442 + fcb034f commit a596a33

File tree

2 files changed

+150
-32
lines changed

2 files changed

+150
-32
lines changed

src/ConvertKit_API.php

Lines changed: 62 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -124,20 +124,8 @@ public function __construct(
124124
$this->access_token = $accessToken;
125125
$this->debug = $debug;
126126

127-
// Set headers.
128-
$headers = [
129-
'Accept' => 'application/json',
130-
'Content-Type' => 'application/json; charset=utf-8',
131-
'User-Agent' => 'ConvertKitPHPSDK/' . self::VERSION . ';PHP/' . phpversion(),
132-
];
133-
if (!empty($this->access_token)) {
134-
$headers['Authorization'] = 'Bearer ' . $this->access_token;
135-
}
136-
137127
// Set the Guzzle client.
138-
$this->client = new Client(
139-
['headers' => $headers]
140-
);
128+
$this->client = new Client();
141129

142130
if ($debug) {
143131
// If no debug log file location specified, define a default.
@@ -243,6 +231,9 @@ public function get_access_token(string $authCode, string $redirectURI)
243231
$request = new Request(
244232
method: 'POST',
245233
uri: $this->oauth_token_url,
234+
headers: $this->request_headers(
235+
auth: false
236+
),
246237
body: (string) json_encode(
247238
[
248239
'code' => $authCode,
@@ -278,6 +269,9 @@ public function refresh_token(string $refreshToken, string $redirectURI)
278269
$request = new Request(
279270
method: 'POST',
280271
uri: $this->oauth_token_url,
272+
headers: $this->request_headers(
273+
auth: false
274+
),
281275
body: (string) json_encode(
282276
[
283277
'refresh_token' => $refreshToken,
@@ -1851,8 +1845,9 @@ public function get_segments(
18511845
* Get markup from ConvertKit for the provided $url.
18521846
*
18531847
* Supports legacy forms and legacy landing pages.
1848+
*
18541849
* Forms and Landing Pages should be embedded using the supplied JS embed script in
1855-
* the API response when using get_resources().
1850+
* the API response when using get_forms() or get_landing_pages().
18561851
*
18571852
* @param string $url URL of HTML page.
18581853
*
@@ -1873,9 +1868,12 @@ public function get_resource(string $url)
18731868

18741869
// Fetch the resource.
18751870
$request = new Request(
1876-
'GET',
1877-
$url,
1878-
['Accept-Encoding' => 'gzip']
1871+
method: 'GET',
1872+
uri: $url,
1873+
headers: $this->request_headers(
1874+
type: 'text/html',
1875+
auth: false
1876+
),
18791877
);
18801878
$response = $this->client->send($request);
18811879

@@ -2100,18 +2098,20 @@ public function make_request(string $endpoint, string $method, array $args = [])
21002098

21012099
$request = new Request(
21022100
method: $method,
2103-
uri: $url
2101+
uri: $url,
2102+
headers: $this->request_headers(),
21042103
);
21052104
break;
21062105

21072106
default:
21082107
$request = new Request(
2109-
method: $method,
2110-
uri: $url,
2111-
body: (string) json_encode($args),
2108+
method: $method,
2109+
uri: $url,
2110+
headers: $this->request_headers(),
2111+
body: (string) json_encode($args),
21122112
);
21132113
break;
2114-
}
2114+
}//end switch
21152115

21162116
// Send request.
21172117
$this->response = $this->client->send(
@@ -2142,4 +2142,44 @@ public function getResponseInterface()
21422142
{
21432143
return $this->response;
21442144
}
2145+
2146+
/**
2147+
* Returns the headers to use in an API request.
2148+
*
2149+
* @param string $type Accept and Content-Type Headers.
2150+
* @param boolean $auth Include authorization header.
2151+
*
2152+
* @since 2.0.0
2153+
*
2154+
* @return array<string,string>
2155+
*/
2156+
private function request_headers(string $type = 'application/json', bool $auth = true)
2157+
{
2158+
$headers = [
2159+
'Accept' => $type,
2160+
'Content-Type' => $type . '; charset=utf-8',
2161+
'User-Agent' => $this->user_agent(),
2162+
];
2163+
2164+
// If no authorization header required, return now.
2165+
if (!$auth) {
2166+
return $headers;
2167+
}
2168+
2169+
// Add authorization header and return.
2170+
$headers['Authorization'] = 'Bearer ' . $this->access_token;
2171+
return $headers;
2172+
}
2173+
2174+
/**
2175+
* Returns the user agent string to use in all HTTP requests.
2176+
*
2177+
* @since 2.0.0
2178+
*
2179+
* @return string
2180+
*/
2181+
private function user_agent()
2182+
{
2183+
return 'ConvertKitPHPSDK/' . self::VERSION . ';PHP/' . phpversion();
2184+
}
21452185
}

tests/ConvertKitAPITest.php

Lines changed: 88 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,94 @@ public function testDebugDisabled()
318318
$this->assertEmpty($this->getLogFileContents());
319319
}
320320

321+
/**
322+
* Test that calling request_headers() returns the expected array of headers
323+
*
324+
* @since 2.0.0
325+
*
326+
* @return void
327+
*/
328+
public function testRequestHeadersMethod()
329+
{
330+
$headers = $this->callPrivateMethod($this->api, 'request_headers', []);
331+
$this->assertArrayHasKey('Accept', $headers);
332+
$this->assertArrayHasKey('Content-Type', $headers);
333+
$this->assertArrayHasKey('User-Agent', $headers);
334+
$this->assertArrayHasKey('Authorization', $headers);
335+
$this->assertEquals($headers['Accept'], 'application/json');
336+
$this->assertEquals($headers['Content-Type'], 'application/json; charset=utf-8');
337+
$this->assertEquals($headers['User-Agent'], 'ConvertKitPHPSDK/' . $this->api::VERSION . ';PHP/' . phpversion());
338+
$this->assertEquals($headers['Authorization'], 'Bearer ' . $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN']);
339+
}
340+
341+
/**
342+
* Test that calling request_headers() with a different `type` parameter
343+
* returns the expected array of headers
344+
*
345+
* @since 2.0.0
346+
*
347+
* @return void
348+
*/
349+
public function testRequestHeadersMethodWithType()
350+
{
351+
$headers = $this->callPrivateMethod($this->api, 'request_headers', [
352+
'type' => 'text/html',
353+
]);
354+
$this->assertArrayHasKey('Accept', $headers);
355+
$this->assertArrayHasKey('Content-Type', $headers);
356+
$this->assertArrayHasKey('User-Agent', $headers);
357+
$this->assertArrayHasKey('Authorization', $headers);
358+
$this->assertEquals($headers['Accept'], 'text/html');
359+
$this->assertEquals($headers['Content-Type'], 'text/html; charset=utf-8');
360+
$this->assertEquals($headers['User-Agent'], 'ConvertKitPHPSDK/' . $this->api::VERSION . ';PHP/' . phpversion());
361+
$this->assertEquals($headers['Authorization'], 'Bearer ' . $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN']);
362+
}
363+
364+
/**
365+
* Test that calling request_headers() with the `auth` parameter set to false
366+
* returns the expected array of headers
367+
*
368+
* @since 2.0.0
369+
*
370+
* @return void
371+
*/
372+
public function testRequestHeadersMethodWithAuthDisabled()
373+
{
374+
$headers = $this->callPrivateMethod($this->api, 'request_headers', [
375+
'auth' => false,
376+
]);
377+
$this->assertArrayHasKey('Accept', $headers);
378+
$this->assertArrayHasKey('Content-Type', $headers);
379+
$this->assertArrayHasKey('User-Agent', $headers);
380+
$this->assertArrayNotHasKey('Authorization', $headers);
381+
$this->assertEquals($headers['Accept'], 'application/json');
382+
$this->assertEquals($headers['Content-Type'], 'application/json; charset=utf-8');
383+
$this->assertEquals($headers['User-Agent'], 'ConvertKitPHPSDK/' . $this->api::VERSION . ';PHP/' . phpversion());
384+
}
385+
386+
/**
387+
* Test that calling request_headers() with a different `type` parameter
388+
* and the `auth` parameter set to false returns the expected array of headers
389+
*
390+
* @since 2.0.0
391+
*
392+
* @return void
393+
*/
394+
public function testRequestHeadersMethodWithTypeAndAuthDisabled()
395+
{
396+
$headers = $this->callPrivateMethod($this->api, 'request_headers', [
397+
'type' => 'text/html',
398+
'auth' => false,
399+
]);
400+
$this->assertArrayHasKey('Accept', $headers);
401+
$this->assertArrayHasKey('Content-Type', $headers);
402+
$this->assertArrayHasKey('User-Agent', $headers);
403+
$this->assertArrayNotHasKey('Authorization', $headers);
404+
$this->assertEquals($headers['Accept'], 'text/html');
405+
$this->assertEquals($headers['Content-Type'], 'text/html; charset=utf-8');
406+
$this->assertEquals($headers['User-Agent'], 'ConvertKitPHPSDK/' . $this->api::VERSION . ';PHP/' . phpversion());
407+
}
408+
321409
/**
322410
* Test that get_oauth_url() returns the correct URL to begin the OAuth process.
323411
*
@@ -4867,8 +4955,6 @@ public function testGetSegmentsPagination()
48674955
*/
48684956
public function testGetResourceLegacyForm()
48694957
{
4870-
$this->markTestIncomplete();
4871-
48724958
$markup = $this->api->get_resource($_ENV['CONVERTKIT_API_LEGACY_FORM_URL']);
48734959

48744960
// Assert that the markup is HTML.
@@ -4887,8 +4973,6 @@ public function testGetResourceLegacyForm()
48874973
*/
48884974
public function testGetResourceLandingPage()
48894975
{
4890-
$this->markTestIncomplete();
4891-
48924976
$markup = $this->api->get_resource($_ENV['CONVERTKIT_API_LANDING_PAGE_URL']);
48934977

48944978
// Assert that the markup is HTML.
@@ -4907,8 +4991,6 @@ public function testGetResourceLandingPage()
49074991
*/
49084992
public function testGetResourceLegacyLandingPage()
49094993
{
4910-
$this->markTestIncomplete();
4911-
49124994
$markup = $this->api->get_resource($_ENV['CONVERTKIT_API_LEGACY_LANDING_PAGE_URL']);
49134995

49144996
// Assert that the markup is HTML.
@@ -4928,8 +5010,6 @@ public function testGetResourceLegacyLandingPage()
49285010
*/
49295011
public function testGetResourceInvalidURL()
49305012
{
4931-
$this->markTestIncomplete();
4932-
49335013
$this->expectException(InvalidArgumentException::class);
49345014
$markup = $this->api->get_resource('not-a-url');
49355015
}
@@ -4944,8 +5024,6 @@ public function testGetResourceInvalidURL()
49445024
*/
49455025
public function testGetResourceInaccessibleURL()
49465026
{
4947-
$this->markTestIncomplete();
4948-
49495027
$this->expectException(ClientException::class);
49505028
$markup = $this->api->get_resource('https://convertkit.com/a/url/that/does/not/exist');
49515029
}

0 commit comments

Comments
 (0)