|
| 1 | +/** |
| 2 | + * @description |
| 3 | + * HTTP code snippet generator for PHP using curl-ext. |
| 4 | + * |
| 5 | + * @author |
| 6 | + * @AhmadNassri |
| 7 | + * |
| 8 | + * for any questions or issues regarding the generated code snippet, please open an issue mentioning the author. |
| 9 | + */ |
| 10 | + |
| 11 | +'use strict' |
| 12 | + |
| 13 | +var util = require('util') |
| 14 | +var helpers = require('./helpers') |
| 15 | + |
| 16 | +module.exports = function (source, options) { |
| 17 | + var opts = util._extend({ |
| 18 | + indent: ' ', |
| 19 | + noTags: false, |
| 20 | + closingTag: false |
| 21 | + }, options) |
| 22 | + |
| 23 | + var code = [] |
| 24 | + var hasBody = false |
| 25 | + |
| 26 | + if (!opts.noTags) { |
| 27 | + code.push('<?php') |
| 28 | + code.push(null) |
| 29 | + } |
| 30 | + |
| 31 | + if (!~helpers.methods.indexOf(source.method.toUpperCase())) { |
| 32 | + code.push(util.format("HttpRequest::methodRegister('%s');", source.method)) |
| 33 | + } |
| 34 | + |
| 35 | + code.push('$client = new http\\Client;') |
| 36 | + code.push('$request = new http\\Client\\Request;') |
| 37 | + code.push(null) |
| 38 | + |
| 39 | + switch (source.postData.mimeType) { |
| 40 | + case 'application/x-www-form-urlencoded': |
| 41 | + code.push('$body = new http\\Message\\Body;') |
| 42 | + code.push(util.format('$body->append(new http\\QueryString(%s));', helpers.convert(source.postData.paramsObj, opts.indent))) |
| 43 | + code.push(null) |
| 44 | + hasBody = true |
| 45 | + break |
| 46 | + |
| 47 | + case 'multipart/form-data': |
| 48 | + var files = [] |
| 49 | + var fields = {} |
| 50 | + |
| 51 | + source.postData.params.forEach(function (param) { |
| 52 | + if (param.fileName) { |
| 53 | + files.push({ |
| 54 | + name: param.name, |
| 55 | + type: param.contentType, |
| 56 | + file: param.fileName, |
| 57 | + data: param.value |
| 58 | + }) |
| 59 | + } else if (param.value) { |
| 60 | + fields[param.name] = param.value |
| 61 | + } |
| 62 | + }) |
| 63 | + |
| 64 | + code.push('$body = new http\\Message\\Body;') |
| 65 | + |
| 66 | + code.push(util.format('$body->addForm(%s, %s);', |
| 67 | + Object.keys(fields).length ? helpers.convert(fields, opts.indent) : 'NULL', |
| 68 | + files.length ? helpers.convert(files, opts.indent) : 'NULL' |
| 69 | + )) |
| 70 | + |
| 71 | + // remove the contentType header |
| 72 | + if (~source.headersObj['content-type'].indexOf('boundary')) { |
| 73 | + delete source.headersObj['content-type'] |
| 74 | + } |
| 75 | + |
| 76 | + code.push(null) |
| 77 | + |
| 78 | + hasBody = true |
| 79 | + break |
| 80 | + |
| 81 | + default: |
| 82 | + if (source.postData.text) { |
| 83 | + code.push('$body = new http\\Message\\Body;') |
| 84 | + code.push(util.format('$body->append(%s);', helpers.convert(source.postData.text))) |
| 85 | + code.push(null) |
| 86 | + hasBody = true |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + code.push(util.format('$request->setRequestUrl(%s);', helpers.convert(source.url))) |
| 91 | + code.push(util.format('$request->setRequestMethod(%s);', helpers.convert(source.method))) |
| 92 | + |
| 93 | + if (hasBody) { |
| 94 | + code.push(util.format('$request->setBody($body);')) |
| 95 | + code.push(null) |
| 96 | + } |
| 97 | + |
| 98 | + if (Object.keys(source.queryObj).length) { |
| 99 | + code.push(util.format('$request->setQuery(new http\\QueryString(%s));', helpers.convert(source.queryObj, opts.indent))) |
| 100 | + code.push(null) |
| 101 | + } |
| 102 | + |
| 103 | + if (Object.keys(source.headersObj).length) { |
| 104 | + code.push(util.format('$request->setHeaders(%s);', helpers.convert(source.headersObj, opts.indent))) |
| 105 | + code.push(null) |
| 106 | + } |
| 107 | + |
| 108 | + if (Object.keys(source.cookiesObj).length) { |
| 109 | + code.push(null) |
| 110 | + code.push(util.format('$client->setCookies(%s);', helpers.convert(source.cookiesObj, opts.indent))) |
| 111 | + code.push(null) |
| 112 | + } |
| 113 | + |
| 114 | + code.push('$client->enqueue($request)->send();') |
| 115 | + code.push('$response = $client->getResponse();') |
| 116 | + code.push(null) |
| 117 | + code.push('echo $response->getBody();') |
| 118 | + |
| 119 | + if (opts.closingTag) { |
| 120 | + code.push(null) |
| 121 | + code.push('?>') |
| 122 | + } |
| 123 | + |
| 124 | + return code.join('\n') |
| 125 | +} |
| 126 | + |
| 127 | +module.exports.info = { |
| 128 | + key: 'http2', |
| 129 | + title: 'HTTP v2', |
| 130 | + link: 'http://devel-m6w6.rhcloud.com/mdref/http', |
| 131 | + description: 'PHP with pecl/http v2' |
| 132 | +} |
0 commit comments