Skip to content

Commit 71a4f2a

Browse files
committed
add: multipart data for php + console.log
1 parent 6983f0a commit 71a4f2a

11 files changed

Lines changed: 110 additions & 4 deletions

File tree

src/targets/php/curl.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,19 @@ module.exports = function (source, options) {
88
noTags: false,
99
maxRedirects: 10,
1010
timeout: 30,
11+
namedErrors: false,
1112
closingTag: false
1213
}, options)
1314

1415
var code = []
1516

1617
if (!opts.noTags) {
1718
code.push('<?php')
18-
code.push('')
19+
code.push(null)
1920
}
2021

2122
code.push('$curl = curl_init();')
22-
code.push('')
23+
code.push(null)
2324

2425
var curlOptions = [{
2526
escape: true,
@@ -90,12 +91,26 @@ module.exports = function (source, options) {
9091
code.push(opts.indent + curlopts.join('\n' + opts.indent))
9192

9293
code.push('));')
93-
code.push('')
94+
code.push(null)
9495
code.push('$response = curl_exec($curl);')
95-
code.push('')
96+
code.push('$err = curl_error($curl);')
97+
code.push(null)
9698
code.push('curl_close($curl);')
99+
code.push(null)
100+
code.push('if ($err) {')
101+
102+
if (opts.namedErrors) {
103+
code.push(opts.indent + 'echo array_flip(get_defined_constants(true)["curl"])[$err];')
104+
} else {
105+
code.push(opts.indent + 'echo "cURL Error #:" . $err;')
106+
}
107+
108+
code.push('} else {')
109+
code.push(opts.indent + 'print_r($response);')
110+
code.push('}')
97111

98112
if (opts.closingTag) {
113+
code.push(null)
99114
code.push('?>')
100115
}
101116

test/fixtures/output/php/curl/application-form-encoded.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,12 @@
1717
));
1818

1919
$response = curl_exec($curl);
20+
$err = curl_error($curl);
2021

2122
curl_close($curl);
23+
24+
if ($err) {
25+
echo "cURL Error #:" . $err;
26+
} else {
27+
print_r($response);
28+
}

test/fixtures/output/php/curl/application-json.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,12 @@
1717
));
1818

1919
$response = curl_exec($curl);
20+
$err = curl_error($curl);
2021

2122
curl_close($curl);
23+
24+
if ($err) {
25+
echo "cURL Error #:" . $err;
26+
} else {
27+
print_r($response);
28+
}

test/fixtures/output/php/curl/cookies.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,12 @@
1414
));
1515

1616
$response = curl_exec($curl);
17+
$err = curl_error($curl);
1718

1819
curl_close($curl);
20+
21+
if ($err) {
22+
echo "cURL Error #:" . $err;
23+
} else {
24+
print_r($response);
25+
}

test/fixtures/output/php/curl/full.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,12 @@
1919
));
2020

2121
$response = curl_exec($curl);
22+
$err = curl_error($curl);
2223

2324
curl_close($curl);
25+
26+
if ($err) {
27+
echo "cURL Error #:" . $err;
28+
} else {
29+
print_r($response);
30+
}

test/fixtures/output/php/curl/headers.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,12 @@
1717
));
1818

1919
$response = curl_exec($curl);
20+
$err = curl_error($curl);
2021

2122
curl_close($curl);
23+
24+
if ($err) {
25+
echo "cURL Error #:" . $err;
26+
} else {
27+
print_r($response);
28+
}

test/fixtures/output/php/curl/multipart-data.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,12 @@
1717
));
1818

1919
$response = curl_exec($curl);
20+
$err = curl_error($curl);
2021

2122
curl_close($curl);
23+
24+
if ($err) {
25+
echo "cURL Error #:" . $err;
26+
} else {
27+
print_r($response);
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
$curl = curl_init();
4+
5+
curl_setopt_array($curl, array(
6+
CURLOPT_URL => "http://mockbin.com/har",
7+
CURLOPT_RETURNTRANSFER => true,
8+
CURLOPT_ENCODING => "",
9+
CURLOPT_MAXREDIRS => 10,
10+
CURLOPT_TIMEOUT => 30,
11+
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
12+
CURLOPT_CUSTOMREQUEST => "POST",
13+
CURLOPT_POSTFIELDS => "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\n\r\n-----011000010111000001101001--",
14+
CURLOPT_HTTPHEADER => array(
15+
"content-type: multipart/form-data; boundary=---011000010111000001101001"
16+
),
17+
));
18+
19+
$response = curl_exec($curl);
20+
$err = curl_error($curl);
21+
22+
curl_close($curl);
23+
24+
if ($err) {
25+
echo "cURL Error #:" . $err;
26+
} else {
27+
print_r($response);
28+
}

test/fixtures/output/php/curl/multipart-form-data.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,12 @@
1717
));
1818

1919
$response = curl_exec($curl);
20+
$err = curl_error($curl);
2021

2122
curl_close($curl);
23+
24+
if ($err) {
25+
echo "cURL Error #:" . $err;
26+
} else {
27+
print_r($response);
28+
}

test/fixtures/output/php/curl/query.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,12 @@
1313
));
1414

1515
$response = curl_exec($curl);
16+
$err = curl_error($curl);
1617

1718
curl_close($curl);
19+
20+
if ($err) {
21+
echo "cURL Error #:" . $err;
22+
} else {
23+
print_r($response);
24+
}

0 commit comments

Comments
 (0)