Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/helpers/__snapshots__/utils.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,12 @@ Array [
"link": "http://php.net/manual/en/book.curl.php",
"title": "cURL",
},
Object {
"description": "PHP with Guzzle",
"key": "guzzle",
"link": "http://docs.guzzlephp.org/en/stable/",
"title": "Guzzle",
},
Object {
"description": "PHP with pecl/http v1",
"key": "http1",
Expand Down
163 changes: 163 additions & 0 deletions src/targets/php/guzzle/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/**
* @description
* HTTP code snippet generator for PHP using Guzzle.
*
* @author @RobertoArruda
* @author @erunion
*
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
*/

import { CodeBuilder } from '../../../helpers/code-builder';
import { getHeader, getHeaderName, hasHeader } from '../../../helpers/headers';
import { Client } from '../../targets';
import { convertType } from '../helpers';

export interface GuzzleOptions {
closingTag?: boolean;
indent?: string;
noTags?: boolean;
shortTags?: boolean;
}

export const guzzle: Client<GuzzleOptions> = {
info: {
key: 'guzzle',
title: 'Guzzle',
link: 'http://docs.guzzlephp.org/en/stable/',
description: 'PHP with Guzzle',
},
convert: ({ postData, fullUrl, method, cookies, headersObj }, options) => {
const opts = {
closingTag: false,
indent: ' ',
noTags: false,
shortTags: false,
...options,
};

const { push, blank, join } = new CodeBuilder({ indent: opts.indent });
const {
code: requestCode,
push: requestPush,
join: requestJoin,
} = new CodeBuilder({ indent: opts.indent });

if (!opts.noTags) {
push(opts.shortTags ? '<?' : '<?php');
blank();
}

switch (postData.mimeType) {
case 'application/x-www-form-urlencoded':
requestPush(
`'form_params' => ${convertType(
postData.paramsObj,
opts.indent + opts.indent,
opts.indent,
)},`,
1,
);
break;

case 'multipart/form-data': {
type MultipartField = {
name: string;
filename?: string;
contents: string | undefined;
headers?: Record<string, string>;
};

const fields: MultipartField[] = [];

if (postData.params) {
postData.params.forEach(function (param) {
if (param.fileName) {
const field: MultipartField = {
name: param.name,
filename: param.fileName,
contents: param.value,
};

if (param.contentType) {
field.headers = { 'Content-Type': param.contentType };
}

fields.push(field);
} else if (param.value) {
fields.push({
name: param.name,
contents: param.value,
});
}
});
}

if (fields.length) {
requestPush(
`'multipart' => ${convertType(fields, opts.indent + opts.indent, opts.indent)}`,
1,
);

// Guzzle adds its own boundary for multipart requests.
if (hasHeader(headersObj, 'content-type')) {
if (getHeader(headersObj, 'content-type')?.indexOf('boundary')) {
const headerName = getHeaderName(headersObj, 'content-type');
if (headerName) {
delete headersObj[headerName];
}
}
}
}
break;
}

default:
if (postData.text) {
requestPush(`'body' => ${convertType(postData.text)},`, 1);
}
}

// construct headers
const headers = Object.keys(headersObj)
.sort()
.map(function (key) {
return `${opts.indent}${opts.indent}'${key}' => '${headersObj[key]}',`;
});

// construct cookies
const cookieString = cookies
.map(cookie => `${encodeURIComponent(cookie.name)}=${encodeURIComponent(cookie.value)}`)
.join('; ');
if (cookieString.length) {
headers.push(`${opts.indent}${opts.indent}'cookie' => '${cookieString}',`);
}

if (headers.length) {
requestPush("'headers' => [", 1);
requestPush(headers.join('\n'));
requestPush('],', 1);
}

push('$client = new \\GuzzleHttp\\Client();');
blank();

if (requestCode.length) {
push(`$response = $client->request('${method}', '${fullUrl}', [`);
push(requestJoin());
push(']);');
} else {
push(`$response = $client->request('${method}', '${fullUrl}');`);
}

blank();
push('echo $response->getBody();');

if (!opts.noTags && opts.closingTag) {
blank();
push('?>');
}

return join();
},
};
15 changes: 15 additions & 0 deletions src/targets/php/guzzle/fixtures/application-form-encoded.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

$client = new \GuzzleHttp\Client();

$response = $client->request('POST', 'http://mockbin.com/har', [
'form_params' => [
'foo' => 'bar',
'hello' => 'world'
],
'headers' => [
'content-type' => 'application/x-www-form-urlencoded',
],
]);

echo $response->getBody();
12 changes: 12 additions & 0 deletions src/targets/php/guzzle/fixtures/application-json.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

$client = new \GuzzleHttp\Client();

$response = $client->request('POST', 'http://mockbin.com/har', [
'body' => '{"number":1,"string":"f\\"oo","arr":[1,2,3],"nested":{"a":"b"},"arr_mix":[1,"a",{"arr_mix_nested":{}}],"boolean":false}',
'headers' => [
'content-type' => 'application/json',
],
]);

echo $response->getBody();
11 changes: 11 additions & 0 deletions src/targets/php/guzzle/fixtures/cookies.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

$client = new \GuzzleHttp\Client();

$response = $client->request('POST', 'http://mockbin.com/har', [
'headers' => [
'cookie' => 'foo=bar; bar=baz',
],
]);

echo $response->getBody();
7 changes: 7 additions & 0 deletions src/targets/php/guzzle/fixtures/custom-method.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

$client = new \GuzzleHttp\Client();

$response = $client->request('PROPFIND', 'http://mockbin.com/har');

echo $response->getBody();
16 changes: 16 additions & 0 deletions src/targets/php/guzzle/fixtures/full.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

$client = new \GuzzleHttp\Client();

$response = $client->request('POST', 'http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value', [
'form_params' => [
'foo' => 'bar'
],
'headers' => [
'accept' => 'application/json',
'content-type' => 'application/x-www-form-urlencoded',
'cookie' => 'foo=bar; bar=baz',
],
]);

echo $response->getBody();
12 changes: 12 additions & 0 deletions src/targets/php/guzzle/fixtures/headers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

$client = new \GuzzleHttp\Client();

$response = $client->request('GET', 'http://mockbin.com/har', [
'headers' => [
'accept' => 'application/json',
'x-foo' => 'Bar',
],
]);

echo $response->getBody();
7 changes: 7 additions & 0 deletions src/targets/php/guzzle/fixtures/https.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

$client = new \GuzzleHttp\Client();

$response = $client->request('GET', 'https://mockbin.com/har');

echo $response->getBody();
14 changes: 14 additions & 0 deletions src/targets/php/guzzle/fixtures/jsonObj-multiline.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

$client = new \GuzzleHttp\Client();

$response = $client->request('POST', 'http://mockbin.com/har', [
'body' => '{
"foo": "bar"
}',
'headers' => [
'content-type' => 'application/json',
],
]);

echo $response->getBody();
12 changes: 12 additions & 0 deletions src/targets/php/guzzle/fixtures/jsonObj-null-value.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

$client = new \GuzzleHttp\Client();

$response = $client->request('POST', 'http://mockbin.com/har', [
'body' => '{"foo":null}',
'headers' => [
'content-type' => 'application/json',
],
]);

echo $response->getBody();
18 changes: 18 additions & 0 deletions src/targets/php/guzzle/fixtures/multipart-data.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

$client = new \GuzzleHttp\Client();

$response = $client->request('POST', 'http://mockbin.com/har', [
'multipart' => [
[
'name' => 'foo',
'filename' => 'hello.txt',
'contents' => 'Hello World',
'headers' => [
'Content-Type' => 'text/plain'
]
]
]
]);

echo $response->getBody();
18 changes: 18 additions & 0 deletions src/targets/php/guzzle/fixtures/multipart-file.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

$client = new \GuzzleHttp\Client();

$response = $client->request('POST', 'http://mockbin.com/har', [
'multipart' => [
[
'name' => 'foo',
'filename' => 'test/fixtures/files/hello.txt',
'contents' => null,
'headers' => [
'Content-Type' => 'text/plain'
]
]
]
]);

echo $response->getBody();
11 changes: 11 additions & 0 deletions src/targets/php/guzzle/fixtures/multipart-form-data-no-params.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

$client = new \GuzzleHttp\Client();

$response = $client->request('POST', 'http://mockbin.com/har', [
'headers' => [
'Content-Type' => 'multipart/form-data',
],
]);

echo $response->getBody();
14 changes: 14 additions & 0 deletions src/targets/php/guzzle/fixtures/multipart-form-data.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

$client = new \GuzzleHttp\Client();

$response = $client->request('POST', 'http://mockbin.com/har', [
'multipart' => [
[
'name' => 'foo',
'contents' => 'bar'
]
]
]);

echo $response->getBody();
7 changes: 7 additions & 0 deletions src/targets/php/guzzle/fixtures/nested.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

$client = new \GuzzleHttp\Client();

$response = $client->request('GET', 'http://mockbin.com/har?foo%5Bbar%5D=baz%2Czap&fiz=buz&key=value');

echo $response->getBody();
7 changes: 7 additions & 0 deletions src/targets/php/guzzle/fixtures/query.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

$client = new \GuzzleHttp\Client();

$response = $client->request('GET', 'http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value');

echo $response->getBody();
7 changes: 7 additions & 0 deletions src/targets/php/guzzle/fixtures/short.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

$client = new \GuzzleHttp\Client();

$response = $client->request('GET', 'http://mockbin.com/har');

echo $response->getBody();
Loading