function call($function, $args, $force_post = 0) { debug("RABX", "RABX calling {$function} via {$this->url}, arguments:", $args); $callstr = rabx_call_string($function, &$args); debug("RABXWIRE", "RABX raw send:", $callstr); if (rabx_is_error($callstr)) { return $callstr; } $c = urlencode($callstr); $post = $this->use_post || $force_post; if (!$post and strlen($u = $this->url . "?{$c}") > 1024) { $post = TRUE; } if ($post) { curl_setopt($this->ch, CURLOPT_URL, $this->url); curl_setopt($this->ch, CURLOPT_POST, 1); curl_setopt($this->ch, CURLOPT_POSTFIELDS, $callstr); } else { curl_setopt($this->ch, CURLOPT_URL, $u); curl_setopt($this->ch, CURLOPT_HTTPGET, 1); /* By default curl passes a "Pragma: no-cache" header. Turn it * off. */ curl_setopt($this->ch, CURLOPT_HTTPHEADER, array("Pragma: ")); } if ($this->userpwd) { curl_setopt($this->ch, CURLOPT_USERPWD, $this->userpwd); } if (!($r = curl_exec($this->ch))) { return rabx_error(RABX_ERROR_TRANSPORT, curl_error($this->ch) . " calling {$this->url}"); } $C = curl_getinfo($this->ch, CURLINFO_HTTP_CODE); debug("RABXWIRE", "RABX raw result:", $r); if ($C != 200) { return rabx_error(RABX_ERROR_TRANSPORT, "HTTP error {$C} calling {$this->url}"); } else { $result = rabx_return_string_parse($r); debug("RABX", "RABX result:", $result); return $result; } }
function mapit_call($url, $params, $opts = array(), $errors = array()) { global $mapit_ch; if (is_null($mapit_ch)) { $mapit_ch = curl_init(); curl_setopt($mapit_ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($mapit_ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); curl_setopt($mapit_ch, CURLOPT_USERAGENT, 'PHP MaPit client'); } if (is_array($params)) { $params = join(',', $params); } if (strpos($url, '/')) { list($urlp, $after) = explode('/', $url, 2); } else { list($urlp, $after) = array($url, ''); } if ($params) { $urlp .= '/' . rawurlencode($params); } if ($after) { $urlp .= "/{$after}"; } if (strlen(OPTION_MAPIT_URL . $urlp) > 1024) { $opts['URL'] = $params; } $qs = ''; foreach ($opts as $k => $v) { if (!$v) { continue; } if (is_array($v)) { $v = join(',', $v); } $qs .= $qs ? ';' : ''; $qs .= rawurlencode($k) . '=' . rawurlencode($v); } if (strlen(OPTION_MAPIT_URL . $urlp) > 1024) { curl_setopt($mapit_ch, CURLOPT_URL, OPTION_MAPIT_URL . $url); curl_setopt($mapit_ch, CURLOPT_POST, 1); curl_setopt($mapit_ch, CURLOPT_POSTFIELDS, $qs); } elseif (strlen(OPTION_MAPIT_URL . "{$urlp}?{$qs}") > 1024) { curl_setopt($mapit_ch, CURLOPT_URL, OPTION_MAPIT_URL . $urlp); curl_setopt($mapit_ch, CURLOPT_POST, 1); curl_setopt($mapit_ch, CURLOPT_POSTFIELDS, $qs); } else { if ($qs) { $urlp .= "?{$qs}"; } curl_setopt($mapit_ch, CURLOPT_URL, OPTION_MAPIT_URL . $urlp); curl_setopt($mapit_ch, CURLOPT_HTTPGET, 1); curl_setopt($mapit_ch, CURLOPT_HTTPHEADER, array("Pragma: ")); } if (!($r = curl_exec($mapit_ch))) { return rabx_error(RABX_ERROR_TRANSPORT, curl_error($mapit_ch) . " calling {$url}"); } $C = curl_getinfo($mapit_ch, CURLINFO_HTTP_CODE); $out = json_decode($r, true); if ($C == 404 && isset($errors['404'])) { return rabx_error($errors['404'], $out['error']); } elseif ($C == 400 && isset($errors['400'])) { return rabx_error($errors['400'], $out['error']); } elseif ($C != 200) { return rabx_error(RABX_ERROR_TRANSPORT, "HTTP error {$C} calling {$url}"); } else { return $out; } }