forked from postmanlabs/httpsnippet-fsless
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcurl.js
More file actions
138 lines (118 loc) · 3.23 KB
/
curl.js
File metadata and controls
138 lines (118 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* @description
* HTTP code snippet generator for PHP using curl-ext.
*
* @author
* @AhmadNassri
*
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
*/
'use strict'
var util = require('util')
var CodeBuilder = require('../../helpers/code-builder')
module.exports = function (source, options) {
var opts = util._extend({
closingTag: false,
indent: ' ',
maxRedirects: 10,
namedErrors: false,
noTags: false,
shortTags: false,
timeout: 30
}, options)
var code = new CodeBuilder(opts.indent)
if (!opts.noTags) {
code.push(opts.shortTags ? '<?' : '<?php')
.blank()
}
code.push('$curl = curl_init();')
.blank()
var curlOptions = [{
escape: true,
name: 'CURLOPT_PORT',
value: source.uriObj.port
}, {
escape: true,
name: 'CURLOPT_URL',
value: source.fullUrl
}, {
escape: false,
name: 'CURLOPT_RETURNTRANSFER',
value: 'true'
}, {
escape: true,
name: 'CURLOPT_ENCODING',
value: ''
}, {
escape: false,
name: 'CURLOPT_MAXREDIRS',
value: opts.maxRedirects
}, {
escape: false,
name: 'CURLOPT_TIMEOUT',
value: opts.timeout
}, {
escape: false,
name: 'CURLOPT_HTTP_VERSION',
value: source.httpVersion === 'HTTP/1.0' ? 'CURL_HTTP_VERSION_1_0' : 'CURL_HTTP_VERSION_1_1'
}, {
escape: true,
name: 'CURLOPT_CUSTOMREQUEST',
value: source.method
}, {
escape: true,
name: 'CURLOPT_POSTFIELDS',
value: source.postData ? source.postData.text : undefined
}]
code.push('curl_setopt_array($curl, array(')
var curlopts = new CodeBuilder(opts.indent, '\n' + opts.indent)
curlOptions.map(function (option) {
if (!~[null, undefined].indexOf(option.value)) {
curlopts.push(util.format('%s => %s,', option.name, option.escape ? JSON.stringify(option.value) : option.value))
}
})
// construct cookies
var cookies = source.cookies.map(function (cookie) {
return encodeURIComponent(cookie.name) + '=' + encodeURIComponent(cookie.value)
})
if (cookies.length) {
curlopts.push(util.format('CURLOPT_COOKIE => "%s",', cookies.join('; ')))
}
// construct cookies
var headers = Object.keys(source.headersObj).sort().map(function (key) {
return util.format('"%s: %s"', key, source.headersObj[key])
})
if (headers.length) {
curlopts.push('CURLOPT_HTTPHEADER => array(')
.push(1, headers.join(',\n' + opts.indent + opts.indent))
.push('),')
}
code.push(1, curlopts.join())
.push('));')
.blank()
.push('$response = curl_exec($curl);')
.push('$err = curl_error($curl);')
.blank()
.push('curl_close($curl);')
.blank()
.push('if ($err) {')
if (opts.namedErrors) {
code.push(1, 'echo array_flip(get_defined_constants(true)["curl"])[$err];')
} else {
code.push(1, 'echo "cURL Error #:" . $err;')
}
code.push('} else {')
.push(1, 'echo $response;')
.push('}')
if (!opts.noTags && opts.closingTag) {
code.blank()
.push('?>')
}
return code.join()
}
module.exports.info = {
key: 'curl',
title: 'cURL',
link: 'http://php.net/manual/en/book.curl.php',
description: 'PHP with ext-curl'
}