|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace KI\CoreBundle\Service; |
|
4
|
|
|
|
|
5
|
|
|
class CurlService |
|
6
|
|
|
{ |
|
7
|
|
|
protected $proxyUrl; |
|
8
|
|
|
protected $proxyUser; |
|
9
|
|
|
|
|
10
|
|
|
public function __construct($proxyUrl, $proxyUser) |
|
11
|
|
|
{ |
|
12
|
|
|
$this->proxyUrl = $proxyUrl; |
|
13
|
|
|
$this->proxyUser = $proxyUser; |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Téléchargement d'une ressource externe |
|
18
|
|
|
* @param string $url Le lien vers la ressource |
|
19
|
|
|
* @param array $payload Le contenu POST éventuel (méthode POST si non nul) |
|
20
|
|
|
* @param array $options Options curl supplémentaire |
|
21
|
|
|
* @return string La réponse |
|
22
|
|
|
*/ |
|
23
|
|
|
public function curl($url, $payload = null, array $options = []) |
|
24
|
|
|
{ |
|
25
|
|
|
// Réglage des options cURL |
|
26
|
|
|
$ch = curl_init(); |
|
27
|
|
|
curl_setopt($ch, CURLOPT_URL, $url); |
|
28
|
|
|
|
|
29
|
|
|
// Nécessaire à cause de la configuration d'Odin |
|
30
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
31
|
|
|
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); |
|
32
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
|
33
|
|
|
curl_setopt($ch, CURLOPT_HEADER, false); |
|
34
|
|
|
curl_setopt($ch, CURLOPT_USERAGENT, 'runscope/0.1'); |
|
35
|
|
|
curl_setopt($ch, CURLOPT_HTTP_VERSION, 'CURL_HTTP_VERSION_1_0'); |
|
36
|
|
|
|
|
37
|
|
|
// Ajout d'éventuels champs POST |
|
38
|
|
|
if (!empty($payload)) { |
|
39
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); |
|
40
|
|
|
curl_setopt($ch, CURLOPT_POST, true); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
// Réglage éventuel du proxy |
|
44
|
|
|
if ($this->proxyUrl !== null) { |
|
45
|
|
|
curl_setopt($ch, CURLOPT_PROXY, $this->proxyUrl); |
|
46
|
|
|
} |
|
47
|
|
|
if ($this->proxyUser !== null) { |
|
48
|
|
|
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $this->proxyUser); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
// On ajoute d'éventuelles options si elles sont spécifiées |
|
52
|
|
|
foreach ($options as $option => $value) { |
|
53
|
|
|
curl_setopt($ch, $option, $value); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
// Récupération de la ressource |
|
57
|
|
|
$data = curl_exec($ch); |
|
58
|
|
|
curl_close($ch); |
|
59
|
|
|
return $data; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|