Commit fd6c7628 authored by Max Koder's avatar Max Koder

[MAJ] cURL

parent d15b15ec
/tootophp/tootophp.json
\ No newline at end of file
/tootophp/tootophp.json
/vendor/
......@@ -10,7 +10,7 @@ if ( $app === false) {
}
//var_dump($app->getUser());
//
//var_dump($app->getFavourites());
//var_dump($app->getAccount(1629));
......
......@@ -62,7 +62,7 @@ class HttpRequest
'POST',
$url,
$headers,
json_encode($params)
$params
);
}
......@@ -105,16 +105,18 @@ class HttpRequest
*/
protected function request($method, $url, $headers = [], $params = [])
{
$opts = $this->getDefaultOpts($method);
if ($method === 'GET' && !empty($params)) {
$url .= '?' . $this->encodeParameters($params);
}
else {
$opts['http']['content'] = $this->encodeParameters($params);
$curl = curl_init();
curl_setopt_array($curl, $this->getOpts($method, $url, $headers, $params));
$response = curl_exec($curl);
if (curl_error($curl) !== '') {
die( 'Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
}
$opts['http']['header'] = $this->encodeHeaders($headers);
$context = stream_context_create($opts);
$response = @file_get_contents($this->domainURL . $url, null, $context);
curl_close($curl);
var_dump($response);
if ($response !== false) {
$json = json_decode($response, true);
}
......@@ -168,27 +170,41 @@ class HttpRequest
}
/**
* Get Defaults Options to create the Stream Context
* Get Options to create the cURL Opts
*
* @param string $method POST or GET
* @param string $url URL
* @param array $headers
* @param array $params
* @return array
*/
protected function getDefaultOpts($method)
protected function getOpts($method, $url, $headers, $params)
{
return [
'http' => [
'content' => '',
'header' => '',
'method' => $method,
'timeout' => 30,
'user_agent' => 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)',
],
'ssl' => [
'ciphers' => 'HIGH',
'verify_host' => false,
'verify_peer' => false,
],
var_dump($headers);
if (isset($headers['Authorization'])) {
$params['access_token'] = str_replace('Bearer ', '', $headers['Authorization']);
echo $params['access_token'];
}
if ($method === 'GET' && !empty($params)) {
$url .= '?' . $this->encodeParameters($params);
$opts = [];
}
else {
$opts = [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $params
];
}
$defaultsOpts = [
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_URL => $this->domainURL . $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 30,
CURLOPT_CONNECTTIMEOUT => 30
];
return $defaultsOpts + $opts;
}
}
......@@ -173,7 +173,7 @@ class TootoPHP
*/
public function authentify($userName, $password)
{
$this->authUser($userName, $password);
return $this->authUser($userName, $password);
}
/**
......@@ -414,15 +414,16 @@ class TootoPHP
protected function authUser($userName = null, $password = null)
{
if (!empty($userName) && !empty($password)) {
if (is_array($this->credentials) && isset($this->credentials['client_id'])) {
var_dump($this->appCredentials);
if (is_array($this->appCredentials) && isset($this->appCredentials['client_id'])) {
// Get authentified token
$token = $this->http->post(
'oauth/token',
$this->headers,
[
'grant_type' => 'password',
'client_id' => $this->credentials['client_id'],
'client_secret' => $this->credentials['client_secret'],
'client_id' => $this->appCredentials['client_id'],
'client_secret' => $this->appCredentials['client_secret'],
'username' => $userName,
'password' => $password,
]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment