This repository was archived by the owner on Dec 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathApi.php
More file actions
104 lines (94 loc) · 3.57 KB
/
Api.php
File metadata and controls
104 lines (94 loc) · 3.57 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
96
97
98
99
100
101
102
103
104
<?php
/**
* Drewberry™
*
* @author Wagner Silva <wagnerjsilva@gmail.com>
* @link <https://twitter.com/wagnerjsilva>
* @version 1.0
*/
namespace CBT;
class Api
{
protected $user;
protected $pass;
protected $url = 'https://crossbrowsertesting.com/api/v3/selenium';
public function __construct($user, $password)
{
$this->setUser($user);
$this->setPass($password);
}
protected function getUser()
{
return $this->user;
}
protected function getPass()
{
return $this->pass;
}
protected function setUser($user)
{
$this->user = $user;
}
protected function setPass($pass)
{
$this->pass = $pass;
}
protected function call($session_id, $method = 'GET', $params = false)
{
$apiResult = new \stdClass();
$process = curl_init();
switch ($method) {
case "POST":
curl_setopt($process, CURLOPT_POST, 1);
if ($params) {
curl_setopt($process, CURLOPT_POSTFIELDS, $params);
curl_setopt($process, CURLOPT_HTTPHEADER, array('User-Agent: php')); //important
}
break;
case "PUT":
curl_setopt($process, CURLOPT_CUSTOMREQUEST, "PUT");
if ($params) {
curl_setopt($process, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($process, CURLOPT_HTTPHEADER, array('User-Agent: php')); //important
}
break;
case 'DELETE':
curl_setopt($process, CURLOPT_CUSTOMREQUEST, "DELETE");
break;
default:
if ($params) {
$this->url = sprintf("%s?%s", $this->url, http_build_query($params));
}
}
// Optional Authentication:
curl_setopt($process, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($process, CURLOPT_USERPWD, $this->getUser().":".$this->getPass());
curl_setopt($process, CURLOPT_URL, $this->url.'/'.$session_id);
curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
$apiResult->content = curl_exec($process);
$apiResult->httpResponse = curl_getinfo($process);
$apiResult->errorMessage = curl_error($process);
$apiResult->params = $params;
curl_close($process);
$paramsString = $params ? http_build_query($params) : '';
$response = json_decode($apiResult->content);
if ($apiResult->httpResponse['http_code'] != 200) {
$message = 'Error calling "'.$apiResult->httpResponse['url'].'" ';
$message .= (isset($paramsString) ? 'with params "'.$paramsString.'" ' : ' ');
$message .= '. Returned HTTP status '.$apiResult->httpResponse['http_code'].' ';
$message .= (isset($apiResult->errorMessage) ? $apiResult->errorMessage : ' ');
$message .= (isset($response->message) ? $response->message : ' ');
die($message);
} else {
$response = json_decode($apiResult->content);
if (isset($response->status)) {
die('Error calling "'.$apiResult->httpResponse['url'].'"'.(isset($paramsString) ? 'with params "'.$paramsString.'"' : '').'". '.$response->message);
}
}
return $response;
}
public function score ($session_id, $score){
return $this->call($session_id, 'PUT', ['score' => $score, 'action' => 'set_score']);
}
}