Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature to set ip resolver type in request options #375

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions examples/ip_resolve_4.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

// First, include Requests
require_once dirname(dirname(__FILE__)) . '/library/Requests.php';

// Next, make sure Requests can load internal classes
Requests::register_autoloader();

// Now let's make a request!
$options = array(
'ipresolve' => 1
);
$request = Requests::get("https://www.google.com", array(), $options);

// Check what we received
var_dump($request->body);
6 changes: 5 additions & 1 deletion library/Requests.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,10 @@ public static function patch($url, $headers, $data = array(), $options = array()
* - `data_format`: How should we send the `$data` parameter?
* (string, one of 'query' or 'body', default: 'query' for
* HEAD/GET/DELETE, 'body' for POST/PUT/OPTIONS/PATCH)
*
* - `ip_resolve` : Allows to select what kind of IP addresses to use when resolving host names.
* This is only interesting when using host names that resolve addresses using more than one version of IP,
* possible values are 0(resolve_whatever), 1(resolve_v4_only), 2(resolve_v6_only), otherwise will get ignored.
* (int, default: 0)
* @throws Requests_Exception On invalid URLs (`nonhttp`)
*
* @param string $url URL to request
Expand All @@ -358,6 +361,7 @@ public static function request($url, $headers = array(), $data = array(), $type
if (empty($options['type'])) {
$options['type'] = $type;
}

$options = array_merge(self::get_default_options(), $options);

self::set_defaults($url, $headers, $data, $type, $options);
Expand Down
6 changes: 6 additions & 0 deletions library/Requests/Transport/cURL.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@ public function request($url, $headers = array(), $data = array(), $options = ar
curl_setopt($this->handle, CURLOPT_SSL_VERIFYHOST, 0);
}

if (isset($options['ipresolve'])) {
if (in_array($options['ipresolve'], array(CURL_IPRESOLVE_WHATEVER, CURL_IPRESOLVE_V4, CURL_IPRESOLVE_V6))){
curl_setopt($this->handle, CURLOPT_IPRESOLVE, $options['ipresolve']);
}
}

curl_exec($this->handle);
$response = $this->response_data;

Expand Down