Bom tenho o seguinte controller onde eu pego as informações de um usuário do twitter pelo nome de usuário usando DOMDocument::loadHTML()
public function info($user = false) {
if ($user === false) {
Url::redirect('get');
}
$url = 'https://twitter.com';
$this->CurlTwitterUserInfo = new CurlTwitterUserInfo($url, $followLocation = true, $timeOut = 30, $maxRedirects = 4, $binaryTransfer = false, $includeHeader = true, $noBody = false);
$this->CurlTwitterUserInfo->setCookieFileLocation(DOCROOT . 'cookies' . DS . $this->CurlTwitterAuth->_authUsername . '.txt');
$this->CurlTwitterUserInfo->setUserAgent($_SERVER['HTTP_USER_AGENT']);
$this->CurlTwitterUserInfo->makeCurlInfo($user);
//var_dump($this->CurlTwitterUserInfo->getHttpStatus());
}
E aqui o meu Helper:
<?php
class CurlTwitterUserInfo {
protected $_userAgent;
protected $_url;
protected $_followLocation;
protected $_timeOut;
protected $_maxRedirects;
protected $_cookie;
protected $_cookieFileLocation = '';
protected $_post;
protected $_postFields;
protected $_referer = '';
protected $_session;
protected $_webPage;
protected $_includeHeader;
protected $_noBody;
protected $_status;
protected $_binaryTransfer;
public $_authentication = 0;
public $_authUsername = '';
public $_authPassword = '';
public function __construct($url, $followLocation = true, $timeOut = 30, $maxRedirects = 4, $binaryTransfer = false, $includeHeader = false, $noBody = false) {
$this->_url = $url;
$this->_followLocation = $followLocation;
$this->_timeOut = $timeOut;
$this->_maxRedirects = $maxRedirects;
$this->_noBody = $noBody;
$this->_includeHeader = $includeHeader;
$this->_binaryTransfer = $binaryTransfer;
}
public function useAuth($use) {
$this->_authentication = 0;
if ($use === true) {
$this->_authentication = 1;
}
}
public function setUsername($username) {
$this->_authUsername = $username;
}
public function setPassword($password) {
$this->_authPassword = $password;
}
public function setReferer($referer) {
$this->_referer = $referer;
}
public function setCookieFileLocation($path) {
$this->_cookieFileLocation = $path;
}
public function setPost($postFields) {
$this->_post = true;
$this->_postFields = $postFields;
}
public function setUserAgent($userAgent) {
$this->_userAgent = $userAgent;
}
public function makeCurlInfo($user) {
$get_user_info = curl_init();
curl_setopt_array($get_user_info, [
CURLOPT_URL => $this->_url . '/' . $user,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_HTTPHEADER => [
"Content-type:text/html;charset=utf-8",
],
CURLOPT_FOLLOWLOCATION => $this->_followLocation,
CURLOPT_USERAGENT => $this->_userAgent,
CURLOPT_COOKIEFILE => $this->_cookieFileLocation,
CURLOPT_COOKIEJAR => $this->_cookieFileLocation,
CURLOPT_COOKIESESSION => true,
CURLOPT_REDIR_PROTOCOLS => CURLPROTO_HTTP | CURLPROTO_HTTPS,
CURLOPT_POSTREDIR => 2,
CURLOPT_AUTOREFERER => 1,
CURLOPT_MAXREDIRS => $this->_maxRedirects,
CURLOPT_ENCODING => "gzip"
]
);
if ($this->_includeHeader) {
curl_setopt_array($get_user_info, [CURLOPT_HEADER => true]);
}
$this->_webPage = curl_exec($get_user_info);
$this->_status = curl_getinfo($get_user_info, CURLINFO_HEADER_SIZE);
$header = substr($this->_webPage, 0, $this->_status);
$body = substr($this->_webPage, $this->_status);
$dom = new DOMDocument('5.0', 'utf-8');
@$dom->loadHTML($body);
$data = json_decode($dom->getElementById('init-data')->getAttribute('value'));
return $data->profile_user;
}
public function getHttpStatus() {
return $this->_status;
}
public function __tostring() {
return $this->_webPage;
}
}
Mas obtenho o seguinte erro:
Error on Aug 23, 2017 19:00PM - DOMDocument::loadHTML(): ID content-main-heading already defined in Entity, line: 1267 in C:\wamp64\www\mvc\system\helpers\CurlTwitterUserInfo.php on line 106
Qual o problema?
Linha 106:
@$dom->loadHTML($body);
<span id="nome" name="nome">
, mesmo sendo HTML válido. Parece que isto pode resolver, pelo menos omitindo o erro: nl3.php.net/manual/en/function.libxml-use-internal-errors.php. Nesse caso você passa a precisar checar por erros manualmente.