m_apiKey = $key; // Set the API key // Open the socket and store handler $this->m_socket = fsockopen($this->m_host, $this->m_port, $this->m_errorNumber, $this->m_errorString); // If socket handler is false... if (!$this->m_socket) { echo "Connection failed"; } } function send($message) { // Write (send) the message to the socket fwrite($this->m_socket, $message); // Start processing the response $response = ''; $contentStarted = false; // Have we got to the response content yet? // While we still have more of the response to read... while (!feof($this->m_socket)) { // Get a line from the response $line = fgets($this->m_socket, 4096); // If line starts with { then we're at the content if (substr($line, 0, 1) === '{') $contentStarted = true; // If we're at the content... if ($contentStarted) { // Append line to the response $response .= $line; } } return $response; } function getRequest($method,$methodArgs) { // Create the URL $url = '/2.0/?'; $url .= 'method='.rawurlencode(trim($method)).'&'; $url .= 'api_key='.rawurlencode(trim($this->m_apiKey)).'&'; foreach ($methodArgs as $name => $value) { $url .= rawurlencode(trim($name)) . '=' . rawurlencode(trim($value)) . '&'; } $url .= 'format=json'; // We want the response content in JSON format // Form the HTTP GET header $out = "GET ".$url." HTTP/1.0\r\n"; $out .= "Host: ".$this->m_host."\r\n"; $out .= "\r\n"; // Make the request and get the JSON response $json = $this->send($out); return json_decode($json, true); } function postRequest($method,$methodArgs) { // Create the URL $url = '/2.0/?'; // Create the data $data = ''; $data .= 'method='.rawurlencode(trim($method)).'&'; $data .= 'api_key='.rawurlencode(trim($this->m_apiKey)).'&'; foreach ($methodArgs as $name => $value) { $data .= rawurlencode(trim($name)) . '=' . rawurlencode(trim($value)) . '&'; } $data .= 'format=json'; // We want the response content in JSON format // Form the HTTP POST header $out = "GET ".$url." HTTP/1.0\r\n"; $out .= "Host: ".$this->m_host."\r\n"; $out .= "Content-Length: ".strlen($data)."\r\n"; $out .= "Content-Type: application/x-www-form-urlencoded\r\n"; $out .= "\r\n"; $out .= $data."\r\n"; // Make the request and get the JSON response $json = $this->send($out); return json_decode($json, true); } }