@@ -18,10 +18,10 @@ function getHeaders (request, indentation) {
18
18
if ( ! _ . isEmpty ( headerArray ) ) {
19
19
headerArray = _ . reject ( headerArray , 'disabled' ) ;
20
20
headerMap = _ . map ( headerArray , function ( header ) {
21
- return `${ indentation } '${ sanitize ( header . key , true ) } ' => ` +
21
+ return `${ indentation } '${ sanitize ( header . key ) } ' => ` +
22
22
`'${ sanitize ( header . value ) } '` ;
23
23
} ) ;
24
- return `$request->setHeaders (array(\n${ headerMap . join ( ',\n' ) } \n));\n` ;
24
+ return `$request->setHeader (array(\n${ headerMap . join ( ',\n' ) } \n));\n` ;
25
25
}
26
26
return '' ;
27
27
}
@@ -76,7 +76,7 @@ self = module.exports = {
76
76
options = { } ;
77
77
}
78
78
if ( ! _ . isFunction ( callback ) ) {
79
- throw new Error ( 'PHP-HttpRequest -Converter: callback is not valid function' ) ;
79
+ throw new Error ( 'PHP-HttpRequest2 -Converter: callback is not valid function' ) ;
80
80
}
81
81
options = sanitizeOptions ( options , self . getOptions ( ) ) ;
82
82
@@ -85,13 +85,26 @@ self = module.exports = {
85
85
indentString = indentString . repeat ( options . indentCount ) ;
86
86
87
87
snippet = '<?php\n' ;
88
- snippet += '$request = new HttpRequest ();\n' ;
88
+ snippet += '$request = new HTTP_Request2 ();\n' ;
89
89
snippet += `$request->setUrl('${ request . url . toString ( ) } ');\n` ;
90
- snippet += `$request->setMethod(HTTP_METH_${ request . method } );\n` ;
91
- if ( options . requestTimeout !== 0 || ! options . followRedirect ) {
90
+ snippet += `$request->setMethod(HTTP_Request2::METHOD_${ request . method } );\n` ;
91
+ if ( options . requestTimeout !== 0 || options . followRedirect ) {
92
+ let configArray = [ ] ;
92
93
snippet += '$request->setOptions(array(' ;
93
- snippet += options . requestTimeout === 0 ? '' : `'timeout' => ${ options . requestTimeout } ` ;
94
- snippet += options . followRedirect ? '' : ', \'redirect\' => false' ;
94
+
95
+ // PHP-HTTP_Request2 method accepts timeout in seconds and it must be an integer
96
+ if ( options . requestTimeout !== 0 && Number . isInteger ( options . requestTimeout / 1000 ) ) {
97
+ let requestTimeout = options . requestTimeout ;
98
+ requestTimeout /= 1000 ;
99
+ configArray . push ( `${ indentString } 'timeout' => ${ requestTimeout } ` ) ;
100
+ }
101
+ if ( options . followRedirect ) {
102
+ configArray . push ( `${ indentString } 'redirect' => TRUE` ) ;
103
+ }
104
+ if ( configArray . length ) {
105
+ snippet += '$request->setOptions(array(\n' ;
106
+ snippet += configArray . join ( ',\n' ) + '\n' ;
107
+ }
95
108
snippet += '));\n' ;
96
109
}
97
110
if ( request . body && ! request . headers . has ( 'Content-Type' ) ) {
@@ -116,9 +129,15 @@ self = module.exports = {
116
129
snippet += `${ parseBody ( request . toJSON ( ) , indentString , options . trimRequestBody ) } ` ;
117
130
}
118
131
snippet += 'try {\n' ;
119
- snippet += `${ indentString } echo $request->send()->getBody();\n` ;
120
- snippet += '} catch(HttpException $ex) {\n' ;
121
- snippet += `${ indentString } echo $ex;\n}` ;
132
+ snippet += `${ indentString } $response = $request->send();\n` ;
133
+ snippet += `${ indentString } if ($response=>getStatus() == 200) {\n` ;
134
+ snippet += `${ indentString . repeat ( 2 ) } echo $response->getBody();\n` ;
135
+ snippet += `${ indentString } } else {\n` ;
136
+ snippet += `${ indentString . repeat ( 2 ) } echo 'Unexpected HTTP status: . $response->getStatus() . ' ' .\n` ;
137
+ snippet += `${ indentString . repeat ( 3 ) } $response->getReasonPhrase();\n` ;
138
+ snippet += `${ indentString } }\n` ;
139
+ snippet += '} catch(HTTP_Request2_Exception $e) {\n' ;
140
+ snippet += `${ indentString } echo 'Error: ' . $e->getMessage();\n}` ;
122
141
return callback ( null , snippet ) ;
123
142
}
124
143
} ;
0 commit comments