How to assign a global variable in PHP class?

Use a wrapper class for cUrl for their needs. So here's the problem, you need to use a proxy , but the problem is that you need to specify the assignment of proxies in each function where there is a request via curl is there any way to make the prod a global variable? the example class below, under the spoiler:
spoiler
<?php

/**
* 
* File wrapper for cURL library
*
*/

class Curl 
{

 const FOLLOWLOCATION = true;
 const RETURNTRANSFER = true;
 const AUTOREFERER = true;
 const TIMEOUT = 30;
 const CONNECT_TIME = 30;

 public $user_agent = null;
 public $proxy;

 private $ch = null;
 private $url = null; 
 private $httpheaders = [];
 private $postparams = null;
 private $proxy_type = null;
 private $cookies = null;


 public function __construct() 
{
 $this--->ch = curl_init();
}

 // Function to POST/GET requests
 public function post($url, $postparams = []) 
{
 $this->url = $url; 
 if (isset($postparams)) { 
 $this->postparams = $postparams;
 } 
 return $this->exec();
}

 // Request header (!)
 public function addHttpheaders($headers)
{
 foreach ($headers as $name => $value) {
 if ($name == 'POST') {
 $this->httpheaders[$name] = $name . '' . $value;
 } else {
 $this->httpheaders[$name] = $name . ': '. $value;
}
}
}

 // Prisvoeniem USER-AGENT
 public function setUserAgent($user_agent = null)
{
 $this->user_agent = $user_agent;
}

 // Connect proxy (HTTPS/SOCKS5)
 public function setProxy($proxy_ip, $proxy_type = ")
{
 $this->proxy = $proxy_ip;
 $this->proxy_type = $proxy_type;
}

 // Temporary name da cookie file
 private function nameCookies($name = null)
{
 for ($i=0; $i<10; ++$i)
 $name .= chr(rand(97, 122));
 return $name . '.temp';
}

 // Create cookie file
 public function setFileCookies()
{
 $this->cookies = $this->nameCookies();
 file_put_contents($this->cookies, "");
}

 // Delete cookie file
 public function clearCookies()
{
unlink($this->cookies);
}

 private function exec()
{
 curl_setopt($this->ch, CURLOPT_URL, $this->url);
 curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION , self::FOLLOWLOCATION);
 curl_setopt($this->ch, CURLOPT_AUTOREFERER, self::AUTOREFERER);
 curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, self::RETURNTRANSFER); 
 curl_setopt($this->ch, CURLOPT_HEADER, true);

 if ($this->postparams)
 curl_setopt($this->ch, CURLOPT_POSTFIELDS, $this->postparams);

 if (count($this->httpheaders))
 curl_setopt($this->ch, CURLOPT_HTTPHEADER, $this->httpheaders);

 if ($this->proxy)
 curl_setopt($this->ch, CURLOPT_PROXY, $this->setProxyIp());

 if ($this->proxy_type == 'socks5')
 curl_setopt($this->ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);

 curl_setopt($this->ch, CURLOPT_TIMEOUT, self::TIMEOUT);
 curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, self::CONNECT_TIME);
 curl_setopt($this->ch, CURLOPT_COOKIEJAR, $this->cookies); // the File to write cookies
 curl_setopt($this->ch, CURLOPT_COOKIEFILE, $this->cookies); // File to read cookies
 $response = curl_exec($this->ch);
 $response = substr($response, curl_getinfo($this->ch, CURLINFO_HEADER_SIZE));

 $this->postparams = null;

 return $response;
}

 public function __destruct()
{
curl_close($this->ch);
}
}

June 8th 19 at 16:29
1 answer
June 8th 19 at 16:31
Solution
When you instantiate a class, pass a proxy to the constructor.
public function __construct($proxy) 
{
 $this->ch = curl_init();
 $this->proxy = $proxy;
 }

and then
$curl = new Curl('127.0.0.1');
$data = $curl->post('http://ya.ru');

But you have the setProxy method, what you do not like? There is no need each time to specify a proxy for this instance of the class, once pointed out and will continue to work with the specified number of times didn't you call the exec method.
$curl = new Curl();
$curl->setProxy('127.0.0.1');

$data1 = $curl->post('http://ya.ru');
$data2 = $curl->post('http://vk.com');
$data3 = $curl->post('http://google.com');

in fact, one line of code more. Well, either I misunderstood your question.
that's not what I think
<?php

/**
* 
* File wrapper for cURL library
*
*/

class Curl 
{

 const FOLLOWLOCATION = true;
 const RETURNTRANSFER = true;
 const AUTOREFERER = true;
 const TIMEOUT = 30;
 const CONNECT_TIME = 30;

 public $user_agent = null;
 public $proxy;

 private $ch = null;
 private $url = null; 
 private $httpheaders = [];
 private $postparams = null;
 private $proxy_type = null;
 private $cookies = null;


public function __construct($proxy) 
{
 $this--->ch = curl_init();
 $this->proxy = $proxy;
}

 // Function to POST/GET requests
 public function post($url, $postparams = []) 
{
 $this->url = $url; 
 if (isset($postparams)) { 
 $this->postparams = $postparams;
 } 
 return $this->exec();
}

 // Request header (!)
 public function addHttpheaders($headers)
{
 foreach ($headers as $name => $value) {
 if ($name == 'POST') {
 $this->httpheaders[$name] = $name . '' . $value;
 } else {
 $this->httpheaders[$name] = $name . ': '. $value;
}
}
}

 // Prisvoeniem USER-AGENT
 public function setUserAgent($user_agent = null)
{
 $this->user_agent = $user_agent;
}

 // Connect proxy (HTTPS/SOCKS5)
 public function setProxy($proxy_ip, $proxy_type = ")
{
 $this->proxy = $proxy_ip;
 $this->proxy_type = $proxy_type;
}

 // Temporary name da cookie file
 private function nameCookies($name = null)
{
 for ($i=0; $i<10; ++$i)
 $name .= chr(rand(97, 122));
 return $name . '.temp';
}

 // Create cookie file
 public function setFileCookies()
{
 $this->cookies = $this->nameCookies();
 file_put_contents($this->cookies, "");
}

 // Delete cookie file
 public function clearCookies()
{
unlink($this->cookies);
}

 private function exec()
{
 curl_setopt($this->ch, CURLOPT_URL, $this->url);
 curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION , self::FOLLOWLOCATION);
 curl_setopt($this->ch, CURLOPT_AUTOREFERER, self::AUTOREFERER);
 curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, self::RETURNTRANSFER); 
 curl_setopt($this->ch, CURLOPT_HEADER, true);

 if ($this->postparams)
 curl_setopt($this->ch, CURLOPT_POSTFIELDS, $this->postparams);

 if (count($this->httpheaders))
 curl_setopt($this->ch, CURLOPT_HTTPHEADER, $this->httpheaders);

 if ($this->proxy)
 curl_setopt($this->ch, CURLOPT_PROXY, $this->setProxyIp());

 if ($this->proxy_type == 'socks5')
 curl_setopt($this->ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);

 curl_setopt($this->ch, CURLOPT_TIMEOUT, self::TIMEOUT);
 curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, self::CONNECT_TIME);
 curl_setopt($this->ch, CURLOPT_COOKIEJAR, $this->cookies); // the File to write cookies
 curl_setopt($this->ch, CURLOPT_COOKIEFILE, $this->cookies); // File to read cookies
 $response = curl_exec($this->ch);
 $response = substr($response, curl_getinfo($this->ch, CURLINFO_HEADER_SIZE));

 $this->postparams = null;

 return $response;
}

 public function __destruct()
{
curl_close($this->ch);
}
}


how correctly to cause?
Ransche I was calling $curl->setProxy('185.82.212.95:8080'); - Kasandra.Robel commented on June 8th 19 at 16:34
I wrote an example
$curl = new Curl('185.82.212.95:8080');
and the setProxy to use only if you need a proxy to change. - violet_Dibbe commented on June 8th 19 at 16:37
now poprobuu. I understand the heirs of this will not apply? - Kasandra.Robel commented on June 8th 19 at 16:40
any heirs, give an example?

I do not understand why to change something, because the setProxy method at any time, you can specify a different proxy to work. Do not need Curl new re-do.

So basically you have everything ready and leave it in the code. - violet_Dibbe commented on June 8th 19 at 16:43
had spars 3 pages

$curl = new Curl();
$curl->setProxy('127.0.0.1');

$data1 = $curl->post('http://ya.ru');
$data2 = $curl->post('http://vk.com');
$data3 = $curl->post('http://google.com');

proxy got ban, you need to change it. Change continue parsite

$curl->setProxy('127.0.0.1');

$data3 = $curl->post('http://habr.ru');
$data4 = $curl->post('http://toster.com');
$data5 = $curl->post('http://geektimes.com');

That is the whole code will look like this
$curl = new Curl();
$curl->setProxy('127.0.0.1');

$data1 = $curl->post('http://ya.ru');
$data2 = $curl->post('http://vk.com');
$data3 = $curl->post('http://google.com');

// here change proxy
$curl->setProxy('127.0.0.1');

$data3 = $curl->post('http://habr.ru');
$data4 = $curl->post('http://toster.com');
$data5 = $curl->post('http://geektimes.com');


In this case return the original form of the constructor, since the proxy using setProxy you will specify. - violet_Dibbe commented on June 8th 19 at 16:46
there's a heir(piece of code)
class AntiCaptcha extends Curl
{

 const ANTI_CAPTCHA_API = 'https://api.anti-captcha.com/';

 // API to work with www.anti-captcha.com
 private function apiCaptcha($method = null, $postparams = null)
{
Curl::addHttpheaders([
 "User-Agent" => $this->user_agent, 
 "Content-Type" => "application/json; charset=utf-8",
 "Accept" =>"application/json"
]);
 $content = $this->post(self::ANTI_CAPTCHA_API . $method, json_encode($postparams));
 $response = json_decode($content, true);
 return $response;
 }
- Kasandra.Robel commented on June 8th 19 at 16:49
will be touch and heirs, to work with them as well as if you worked directly with Curl class.

$curl = new AntiCaptcha();
$curl->setProxy('127.0.0.1');
$result = $curl->apiCaptcha();
- violet_Dibbe commented on June 8th 19 at 16:52
thanks I will dig and do) - Kasandra.Robel commented on June 8th 19 at 16:55
Only method Curl::addHttpheaders you have not static, it should be caused so
$this->addHttpheaders
If you are going to call as static, its use will not lead to any changes, you just use it dry and it will have no affect.

ApiCaptcha and the method must be public, otherwise you will not be able to call it from outside the class AntiCaptcha - violet_Dibbe commented on June 8th 19 at 16:58
ApiCaptcha and the method must be public, otherwise you will not be able to call it from outside the class AntiCaptcha

thank you and it is not necessary to call on a class, it is necessary in the class AntiCaptcha. - Kasandra.Robel commented on June 8th 19 at 17:01
what is the problem if use functions do not work your example:
class cURL
spoiler
<?php

/**
* 
* File wrapper for cURL library
*
*/

class Curl 
{

 const FOLLOWLOCATION = true;
 const RETURNTRANSFER = true;
 const AUTOREFERER = true;
 const TIMEOUT = 30;
 const CONNECT_TIME = 30;

 public $user_agent = null;
 public $proxy;

 private $ch = null;
 private $url = null; 
 private $httpheaders = [];
 private $postparams = null;
 private $proxy_type = null;
 private $cookies = null;


public function __construct($proxy) 
{
 $this--->ch = curl_init();
 $this->proxy = $proxy;
}

 // Function to POST/GET requests
 public function post($url, $postparams = []) 
{
 $this->url = $url; 
 if (isset($postparams)) { 
 $this->postparams = $postparams;
 } 
 return $this->exec();
}

 // Request header (!)
 public function addHttpheaders($headers)
{
 foreach ($headers as $name => $value) {
 if ($name == 'POST') {
 $this->httpheaders[$name] = $name . '' . $value;
 } else {
 $this->httpheaders[$name] = $name . ': '. $value;
}
}
}

 // Prisvoeniem USER-AGENT
 public function setUserAgent($user_agent = null)
{
 $this->user_agent = $user_agent;
}

 // Connect proxy (HTTPS/SOCKS5)
 public function setProxy($proxy_ip, $proxy_type = ")
{
 $this->proxy = $proxy_ip;
 $this->proxy_type = $proxy_type;
}

 // Temporary name da cookie file
 private function nameCookies($name = null)
{
 for ($i=0; $i<10; ++$i)
 $name .= chr(rand(97, 122));
 return $name . '.temp';
}

 // Create cookie file
 public function setFileCookies()
{
 $this->cookies = $this->nameCookies();
 file_put_contents($this->cookies, "");
}

 // Delete cookie file
 public function clearCookies()
{
unlink($this->cookies);
}

 private function exec()
{
 curl_setopt($this->ch, CURLOPT_URL, $this->url);
 curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION , self::FOLLOWLOCATION);
 curl_setopt($this->ch, CURLOPT_AUTOREFERER, self::AUTOREFERER);
 curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, self::RETURNTRANSFER); 
 curl_setopt($this->ch, CURLOPT_HEADER, true);

 if ($this->postparams)
 curl_setopt($this->ch, CURLOPT_POSTFIELDS, $this->postparams);

 if (count($this->httpheaders))
 curl_setopt($this->ch, CURLOPT_HTTPHEADER, $this->httpheaders);

 if ($this->proxy)
 curl_setopt($this->ch, CURLOPT_PROXY, $this->setProxyIp());

 if ($this->proxy_type == 'socks5')
 curl_setopt($this->ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);

 curl_setopt($this->ch, CURLOPT_TIMEOUT, self::TIMEOUT);
 curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, self::CONNECT_TIME);
 curl_setopt($this->ch, CURLOPT_COOKIEJAR, $this->cookies); // the File to write cookies
 curl_setopt($this->ch, CURLOPT_COOKIEFILE, $this->cookies); // File to read cookies
 $response = curl_exec($this->ch);
 $response = substr($response, curl_getinfo($this->ch, CURLINFO_HEADER_SIZE));

 $this->postparams = null;

 return $response;
}

 public function __destruct()
{
curl_close($this->ch);
 }
it is 1 the Parser class
spoiler
<?php
class Parser
{

 private $curl;
 private $anticaptcha;

 public function __construct ()
{
 $this--->curl = new Curl();
 } 

 // Change the privacy on the website www.vk.com
 private function setPrivate()
{
 //more code here does not matter
 $this->curl->post('***');//there is already a proxy not substituted
}

 public function test()
{
 return $this->curl->post('http://httpbin.org/ip');//there is already a proxy not substituted
}
}


the startup file
<?php
spl_autoload_register(function ($class) {
 require_once $class . '.php';
});
$curl = new Curl();
$pr= new Parser();
$curl--->setProxy('185.82.212.95:8080');
$pr->test();
- Kasandra.Robel commented on June 8th 19 at 17:04
you have different instances that are not related, and therefore not working. Try this
public function setProxy($proxy)
{
$this->curl->setProxy($proxy);
}


<?php
spl_autoload_register(function ($class) {
 require_once $class . '.php';
});

$pr= new Parser();
$pr--->setProxy('185.82.212.95:8080');
$pr->test();
- violet_Dibbe commented on June 8th 19 at 17:07
Still possible to do so

<?php
spl_autoload_register(function ($class) {
 require_once $class . '.php';
});
$curl = new Curl();
$pr= new Parser();
$pr--->setCurl($curl);
$pr->curl->setProxy('185.82.212.95:8080');
$pr->test();

 ... 
// Parser.php
public $curl;

public function setCurl($curl)
{
 //more code here does not matter
 $this->curl = $curl;
}
- violet_Dibbe commented on June 8th 19 at 17:10
thanks , this is certainly such crutches, but it works. I understand it in each inherited class needs to be done?) - Kasandra.Robel commented on June 8th 19 at 17:13
if you inherit, then no. Only in the class-the parent and all of its methods except private, will be accessible in derived classes (if they are not overridden in the inherited class). - violet_Dibbe commented on June 8th 19 at 17:16

Find more questions by tags PHP