@evgeniy0090

В чем причина пустого ответа в Curl обертке?

Доброго времени суток знатоки. Пишу свой велосипед точнее обертку на curl, вот код:
<?php

/**
* Curl-обертка
**/

class Curl
{
  protected static $curl;

  private $ch;
  private $user_agent;
  private $url;
  private $proxy;
  private $proxy_type;

  public function __construct($url = null)
  {
    $this->ch = curl_init();
    if (isset($url)) {
      $this->url = $url;
    }
  }

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

  public static function init($url = null)
  {
    return (is_null(self::$curl)) ?
      self::$curl = new self($url) :
      self::$curl;
  }

  /**
  * Установка user-agent
  **/
  public function setUserAgent($user_agent)
  {
    $this->user_agent = $user_agent;
    return self::$curl;
  }

  /**
  * Установка заголовка
  **/
  public function setHttpHeader($header, $ajax = false)
  {
    if (isset($this->user_agent) == true) {
      $this->http_header = ["User-Agent" => "User-Agent: {$this->user_agent}"];
    }

    foreach ($header as $name => $value) {
      $this->http_header[$name] = $name . ': ' . $value;
    }

    if ($ajax) {
      $this->http_header += ["X-Requested-With" => "X-Requested-With: XMLHttpRequest"];
    }

    return self::$curl;
  }

  public function exec()
  {
    curl_setopt($this->ch, CURLOPT_URL, $this->url);
    curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION , true);
    curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
if (isset($this->httpheaders)) {
      curl_setopt($this->ch, CURLOPT_HTTPHEADER, $this->http_header);
    }
    curl_setopt($this->ch, CURLOPT_COOKIEJAR, '1.txt');
    curl_setopt($this->ch, CURLOPT_COOKIEFILE, '1.txt');
    curl_setopt($this->ch, CURLOPT_TIMEOUT, 60);
    curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, 30);

    $response = curl_exec($this->ch);
    return $response;
  }
}


пробую вызвать его
echo Curl::init('http://httpbin.org/ip')->exec();

прекрасно отрабатывает, тут вопросов нету. Затем пытаюсь установить user-agent и http-заголовок чтобы не дублировать карждый раз заного, вот так:
$header = [
  "Accept"          =>  "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
  "Accept-Language" =>  "ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3",
  "Content-Type"    =>  "application/x-www-form-urlencoded"
];

Curl::init()->setUserAgent('Mozilla/5.0 (Windows NT 10.0; rv:78.0) Gecko/20100101 Firefox/78.0');
Curl::init()->setHttpHeader($header);
echo Curl::init('http://httpbin.org/ip')->exec();


в ответ тишина, как будто url не присваевается. что не так делаю?
  • Вопрос задан
  • 50 просмотров
Пригласить эксперта
Ответы на вопрос 1
dimonchik2013
@dimonchik2013
non progredi est regredi
отладку включи

VERBOSE True
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы
Ведисофт Екатеринбург
от 25 000 ₽
YCLIENTS Москва
от 200 000 до 350 000 ₽
от 300 000 до 500 000 ₽
06 мая 2024, в 12:36
30000 руб./за проект
06 мая 2024, в 12:22
5000 руб./за проект
06 мая 2024, в 12:19
1500 руб./в час