private function _add($url, $function, $opts) { if (!empty($this['session'])) { $oCurl = $this['session']; unset($this['session']); } else { $oCurl = new CurlHelper(); } $oCurl->setOptions($url, $function, $opts); $this->add($oCurl); return $oCurl; }
/** * Make a request to the database webservice. * * @param string $method The HTTP method to use [POST, PUT, GET]. * @param string $url The URL to send the request to. * @param array|string $data The data to send along with the request. * * @return mixed the answer from the database webservice */ protected function request($method, $url, $data = null) { switch ($method) { case "POST": $this->curl->setOption(CURLOPT_POST, 1); if ($data) { $this->curl->setOption(CURLOPT_POSTFIELDS, $data); } break; case "PUT": $this->curl->setOption(CURLOPT_CUSTOMREQUEST, "PUT"); if ($data) { $this->curl->setOption(CURLOPT_POSTFIELDS, http_build_query($data)); } break; default: if ($data) { $url = sprintf("%s?%s", $url, http_build_query($data)); } } $this->curl->setOption(CURLOPT_URL, $url); $this->curl->setOption(CURLOPT_RETURNTRANSFER, 1); $result = $this->curl->execute(); $this->curl->close(); return $result; }
private function applyCurlOptions() { if ($this->curlOptions) { foreach ($this->curlOptions as $optionName => $optionValue) { $this->curlHelper->setCurlOption(constant($optionName), $optionValue); } } }
/** * @param string $apiName * @param array $params * @return mixed Contents of field response * @throws Exception */ public function makeApiCall($apiName, $params) { $query = array_merge(array('access_token' => $this->accessToken), $params); $queryString = http_build_query($query); $answer = CurlHelper::getUrl("https://api.vk.com/method/{$apiName}?{$queryString}", array(CURLOPT_SSL_VERIFYPEER => false)); $answer = json_decode($answer, true); if (isset($answer['error']) || !isset($answer['response'])) { throw new Exception(print_r($answer, true)); } return $answer['response']; }
public function makeRequest($service, $requestBody) { $url = 'http://www.webservicex.net/' . $service . '.asmx'; $request = '<?xml version="1.0" encoding="utf-8"?> <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:Body>' . $requestBody . '</soap12:Body> </soap12:Envelope>'; $options = [CURLOPT_SSL_VERIFYHOST => 1, CURLOPT_SSL_VERIFYPEER => 1, CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => 1, CURLOPT_CONNECTTIMEOUT => 60, CURLOPT_POST => 1, CURLOPT_POSTFIELDS => $request, CURLOPT_HTTPHEADER => ['Content-Type: text/xml; charset=utf-8', 'Content-Length: ' . strlen($request)], CURLOPT_FOLLOWLOCATION => true]; $this->response = CurlHelper::makeRequest($url, $options); return $this; }
private function readXml($curlH) { $bPassages = array(); $resp = null; $sCode = CurlHelper::checkHttpStatus($curlH->getHttpStatus()); switch ($sCode) { case constant("STATUS_SUCCESS"): $resp = simplexml_load_string($curlH->getResponse(), 'SimpleXMLElement', LIBXML_NOCDATA); break; case constant("STATUS_CLIENT_ERROR"): $this->_isError = true; $this->_errorMessage = "request was invalid"; break; case constant("STATUS_SERVER_ERROR"): $this->_isError = true; $this->_errorMessage = "server encountered an error"; break; case constant("STATUS_UNKNOWN_ERROR"): default: $this->_isError = true; $this->_errorMessage = "something awful happened"; } if (!empty($resp)) { $this->_rawResponse = $resp; $result = $resp->search->result; if (!empty($result)) { //was too early on the first try $passages = $result->passages->passage; if (!empty($passages)) { foreach ($passages as $passage) { if (!empty($passage)) { $bpObject = new BiblePassageObject(); $bpObject->set_display($passage->display); $bpObject->set_version($passage->version); $bpObject->set_textStr($passage->text); $bpObject->set_copyright($passage->copyright); $bpObject->set_fums($resp->meta->fums); $bPassages[] = $bpObject; } else { $this->_isError = true; $this->_errorMessage = "passage was empty"; } } } } else { $this->_isError = true; $this->_errorMessage = "result set was empty"; } } return $bPassages; }
public function translate($text, $fromLang, $toLang) { $textArray = TextSplitter::explode($text); $urls = []; foreach ($textArray as $text) { $urls[] = $this->rootURL . $this->translatePath . '?' . http_build_query(['lang' => $fromLang . '-' . $toLang, 'text' => $text]); } $translateArray = []; foreach (CurlHelper::multi($urls) as $rawTranslate) { $rawTranslate = trim($rawTranslate, '"'); $translateArray[] = str_replace('\\n', $this->eolSymbol, $rawTranslate); } return TextSplitter::implode($translateArray); }
protected function parseGalleryPage($url) { $doc = new DOMDocument(); $doc->loadHTML(file_get_contents($this->base_url . $url)); $xpath = new DOMXPath($doc); $gallery_url = $xpath->query("//div[@class='content img_list']//a/@href")->item(0)->nodeValue; $doc->loadHTML(file_get_contents($this->base_url . $gallery_url)); $xpath = new DOMXPath($doc); //get urls on photo page $urls = []; foreach ($xpath->query("//ul[@id='photo_carousel']//a/@href") as $attr) { $urls[] = $this->base_url . $attr->nodeValue; } $imgs = []; foreach (CurlHelper::multi($urls) as $url => $html) { if ($html) { $imgs[] = $this->parseImgPage($html); } } return $imgs; }
/** * Validates a single attribute. * This method should be overridden by child classes. * @param CModel $object the data object being validated * @param string $attribute the name of the attribute to be validated. */ protected function validateAttribute($object, $attribute) { $value = $object->{$attribute}; $xml = false; if ($this->useCache && !empty(self::$filesCache[$value])) { $xmlFile = self::$filesCache[$value]; } else { $xmlFile = tempnam(Yii::app()->getRuntimePath(), 'xml'); try { CurlHelper::downloadToFile($value, $xmlFile); self::$filesCache[$value] = $xmlFile; } catch (Exception $e) { } } if (file_exists($xmlFile)) { $xml = @simplexml_load_file($xmlFile); } if ($xml === false) { $message = $this->message !== null ? $this->message : Yii::t('ValidXml.app', '{attribute} doesn\'t contain valid XML.'); $this->addError($object, $attribute, $message); } }