Skip to content

Commit

Permalink
Merge pull request #233 from s3ri0usman/master
Browse files Browse the repository at this point in the history
Added ability to use proxy for requests
  • Loading branch information
raiym authored Dec 8, 2017
2 parents f4295ef + 4c14ffc commit d02de7e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ $instagram->login(); // will use cached session if you can force login $instagra
$account = $instagram->getAccountById(3);
echo $account->getUsername();
```
Using proxy for requests:

```php
$instagram = new Instagram();
Instagram::setProxy([
'address' => '111.112.113.114',
'port' => '8080',
'tunnel' => true,
'timeout' => 30,
]);
// Request with proxy
$account = $instagram->getAccount('kevin');
Instagram::disableProxy();
// Request without proxy
$account = $instagram->getAccount('kevin');
```

## Installation

Expand Down
38 changes: 38 additions & 0 deletions src/InstagramScraper/Instagram.php
Original file line number Diff line number Diff line change
Expand Up @@ -1044,4 +1044,42 @@ public function saveSession()
$cachedString->set($this->userSession);
}

/**
* @param array $config
*/
public static function setProxy(array $config)
{
$defaultConfig = [
'port' => false,
'tunnel' => false,
'address' => false,
'type' => CURLPROXY_HTTP,
'timeout' => false,
'auth' => [
'user' => '',
'pass' => '',
'method' => CURLAUTH_BASIC
],
];

$config = array_replace($defaultConfig, $config);

Request::proxy($config['address'], $config['port'], $config['type'], $config['tunnel']);

if (isset($config['auth'])) {
Request::proxyAuth($config['auth']['user'], $config['auth']['pass'], $config['auth']['method']);
}

if (isset($config['timeout'])) {
Request::timeout((int)$config['timeout']);
}
}

/**
* Disable proxy for all requests
*/
public static function disableProxy()
{
Request::proxy('');
}
}

1 comment on commit d02de7e

@Sieboldianus
Copy link

@Sieboldianus Sieboldianus commented on d02de7e Dec 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found it simpler just to add the following code in my script:
Unirest\Request::proxy('379.xx.x.x', 2x28, CURLPROXY_HTTP);
2x28 = port
379.xx.x.x = ip

Please sign in to comment.