Support proxy, proxy-user, max-time, location on PHP (#354)

This commit is contained in:
Ekin Karadeniz
2022-03-21 20:12:03 +03:00
committed by GitHub
parent 7595e8b94f
commit aaaf13189b
17 changed files with 84 additions and 3 deletions

View File

@@ -0,0 +1 @@
curl 'http://example.net' -L

View File

@@ -0,0 +1 @@
curl 'http://example.com' -m 20

View File

@@ -0,0 +1 @@
curl 'http://localhost:9000' -x 'http://localhost:8080'

View File

@@ -0,0 +1 @@
curl 'http://localhost:9000' --proxy 'http://localhost:8080' -U 'anonymous:anonymous'

10
fixtures/php/follow_location.php generated Normal file
View File

@@ -0,0 +1,10 @@
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://example.net');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$response = curl_exec($ch);
curl_close($ch);

11
fixtures/php/get_digest_auth.php generated Normal file
View File

@@ -0,0 +1,11 @@
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost:28139/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($ch, CURLOPT_USERPWD, 'some_username:some_password');
$response = curl_exec($ch);
curl_close($ch);

10
fixtures/php/get_max_time.php generated Normal file
View File

@@ -0,0 +1,10 @@
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://example.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$response = curl_exec($ch);
curl_close($ch);

10
fixtures/php/get_proxy.php generated Normal file
View File

@@ -0,0 +1,10 @@
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost:9000');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_PROXY, 'http://localhost:8080');
$response = curl_exec($ch);
curl_close($ch);

11
fixtures/php/get_proxy_with_auth.php generated Normal file
View File

@@ -0,0 +1,11 @@
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost:9000');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_PROXY, 'http://localhost:8080');
curl_setopt($ch, CURLOPT_PROXYUSERPWD, 'anonymous:anonymous');
$response = curl_exec($ch);
curl_close($ch);

View File

@@ -42,7 +42,8 @@ export const _toPhp = request => {
}
if (request.auth) {
phpCode += 'curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);\n'
const authType = request.digest ? 'CURLAUTH_DIGEST' : 'CURLAUTH_BASIC'
phpCode += 'curl_setopt($ch, CURLOPT_HTTPAUTH, ' + authType + ');\n'
phpCode += "curl_setopt($ch, CURLOPT_USERPWD, '" + quote(request.auth.join(':')) + "');\n"
}
@@ -58,14 +59,29 @@ export const _toPhp = request => {
}
}
requestDataCode += ']'
} else if (request.isDataBinary) {
requestDataCode += "file_get_contents('" + quote(request.data.substring(1)) + "')"
} else if (request.isDataBinary && request.data.charAt(0) === '@') {
requestDataCode = "file_get_contents('" + quote(request.data.substring(1)) + "')"
} else {
requestDataCode = "'" + quote(request.data) + "'"
}
phpCode += 'curl_setopt($ch, CURLOPT_POSTFIELDS, ' + requestDataCode + ');\n'
}
if (request.proxy) {
phpCode += "curl_setopt($ch, CURLOPT_PROXY, '" + quote(request.proxy) + "');\n"
if (request.proxyAuth) {
phpCode += "curl_setopt($ch, CURLOPT_PROXYUSERPWD, '" + quote(request.proxyAuth) + "');\n"
}
}
if (request.timeout) {
phpCode += 'curl_setopt($ch, CURLOPT_TIMEOUT, ' + (parseInt(request.timeout) || 0) + ');\n'
}
if (request.followRedirects) {
phpCode += 'curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);\n'
}
if (request.insecure) {
phpCode += 'curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);\n'
phpCode += 'curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);\n'

View File

@@ -1132,6 +1132,15 @@ const buildRequest = parsedArguments => {
}
if (parsedArguments.proxy) {
request.proxy = parsedArguments.proxy
if (parsedArguments['proxy-user']) {
request.proxyAuth = parsedArguments['proxy-user']
}
}
if (parsedArguments['max-time']) {
request.timeout = parsedArguments['max-time']
}
if (parsedArguments.location) {
request.followRedirects = true
}
if (parsedArguments.output) {
request.output = parsedArguments.output