Request.php
2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
/**
* @package AllediaFramework
* @contact www.alledia.com, hello@alledia.com
* @copyright 2016 Alledia.com, All rights reserved
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
*/
namespace Alledia\Framework\Network;
defined('_JEXEC') or die();
class Request
{
/**
* post
* POST request
*
* @access public
* @param string $url - url
* @param array $data - post data
* @return string
*/
public function post($url, $data = array())
{
if ($this->hasCURL()) {
return $this->postCURL($url, $data);
} else {
return $this->postFOpen($url, $data);
}
}
/**
* hasCURL
* Does the server have the curl extension ?
*
* @access protected
* @return boolean
*/
protected function hasCURL()
{
return function_exists('curl_init');
}
/**
* postCURL
* POST request with curl
*
* @access protected
* @param string $url - url
* @param array $data - post data
* @return string
*/
protected function postCURL($url, $data = array())
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($data));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec($ch);
curl_close($ch);
return $contents;
}
/**
* postFOpen
* POST request with fopen
*
* @access protected
* @param string $url - url
* @param array $data - post data
* @return string
*/
protected function postFOpen($url, $data = array())
{
$stream = fopen($url, 'r', false, stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => http_build_query(
$data
)
)
)));
$contents = stream_get_contents($stream);
fclose($stream);
return $contents;
}
}