/** * Makes a request to the RESTful server, and return a {@link RestfulService_Response} object for parsing of the result. * @todo Better POST, PUT, DELETE, and HEAD support * @todo Caching of requests - probably only GET and HEAD requestst * @todo JSON support in RestfulService_Response * @todo Pass the response headers to RestfulService_Response * * This is a replacement of {@link connect()}. * * @return RestfulService_Response - If curl request produces error, the returned response's status code will be 500 */ public function request($subURL = '', $method = "GET", $data = null, $headers = null, $curlOptions = array()) { $url = $this->getAbsoluteRequestURL($subURL); $method = strtoupper($method); assert(in_array($method, array('GET', 'POST', 'PUT', 'DELETE', 'HEAD', 'OPTIONS'))); $cachedir = TEMP_FOLDER; // Default silverstripe cache $cache_file = md5($url); // Encoded name of cache file $cache_path = $cachedir . "/xmlresponse_{$cache_file}"; // Check for unexpired cached feed (unless flush is set) if (!isset($_GET['flush']) && @file_exists($cache_path) && @filemtime($cache_path) + $this->cache_expire > time()) { $store = file_get_contents($cache_path); $response = unserialize($store); } else { $ch = curl_init(); $timeout = 5; $useragent = "SilverStripe/" . SapphireInfo::Version(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_USERAGENT, $useragent); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); // Add headers if ($this->customHeaders) { $headers = array_merge((array) $this->customHeaders, (array) $headers); } if ($headers) { curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); } // Add authentication if ($this->authUsername) { curl_setopt($ch, CURLOPT_USERPWD, "{$this->authUsername}:{$this->authPassword}"); } // Add fields to POST requests if ($method == 'POST') { curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } // Apply proxy settings if (is_array($this->proxy)) { curl_setopt_array($ch, $this->proxy); } // Set any custom options passed to the request() function curl_setopt_array($ch, $curlOptions); // Run request curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $responseBody = curl_exec($ch); $curlError = curl_error($ch); // Problem verifying the server SSL certificate; just ignore it as it's not mandatory if (strpos($curlError, '14090086') !== false) { curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $responseBody = curl_exec($ch); $curlError = curl_error($ch); } $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($curlError !== '' || $statusCode == 0) { $statusCode = 500; } $response = new RestfulService_Response($responseBody, $statusCode); curl_close($ch); if ($curlError === '' && !$response->isError()) { // Serialise response object and write to cache $store = serialize($response); file_put_contents($cache_path, $store); } else { // In case of curl or/and http indicate error, populate response's cachedBody property // with cached response body with the cache file exists if (@file_exists($cache_path)) { $store = file_get_contents($cache_path); $cachedResponse = unserialize($store); $response->setCachedBody($cachedResponse->getBody()); } else { $response->setCachedBody(false); } } } return $response; }
protected function parseResults(RestfulService_Response $results) { $data = new DataObjectSet(); // Parse out items in the request element so we can create pager and other items. $request = $results->xpath('/shrs/rq'); $request = $request[0]; $rqTitle = $request->xpath('t'); $rqDate = $request->xpath('dt'); $rqStart = $request->xpath('si'); $rqCount = $request->xpath('rpd'); $rqTotal = $request->xpath('tr'); $rqTotalViewable = $request->xpath('tv'); $rqTotalViewable = intval($rqTotalViewable[0]); $this->searchTitle = strval($rqTitle[0]); $records = $results->xpath('/shrs/rs/r'); $zebra = 0; foreach ($records as $rec) { // create an empty dataobject to hold the job data $job = array(); // get each data node from the xml $title = $rec->xpath('jt'); $company = $rec->xpath('cn'); $source = $rec->xpath('src'); $type = $rec->xpath('ty'); $location = $rec->xpath('loc'); $seen = $rec->xpath('ls'); $posted = $rec->xpath('dp'); $excerpt = $rec->xpath('e'); // create new fields for each piece of data in the dataobject $job['ID'] = new Text(NULL); $job['ID']->setValue(md5(strval($title[0]))); $job['Title'] = new Text(NULL); $job['Title']->setValue(strval($title[0])); $job['Company'] = new Text(NULL); $job['Company']->setValue(strval($company[0])); $job['Source'] = new Text(NULL); // Need to grab the url attribute from the source element. $srcAttribs = $source[0]->attributes(); $job['Source']->setValue(strval($srcAttribs['url'])); $job['Location'] = new Text(NULL); $job['Location']->setValue(strval($location[0])); $job['LastSeen'] = new SS_DateTime(); $job['LastSeen']->setValue(strval($seen[0])); $job['DatePosted'] = new SS_DateTime(); $job['DatePosted']->setValue(strval($posted[0])); $job['Excerpt'] = new HTMLText(); $job['Excerpt']->setValue('<p>' . strval($excerpt[0]) . '</p>'); $job['Zebra'] = !$zebra ? 'odd' : 'even'; $zebra = !$zebra ? 1 : 0; $data->push(new ArrayData($job)); unset($job, $title, $company, $source, $type, $location, $seen, $posted, $excerpt); } $data->setPageLimits($this->page, $this->window, $rqTotalViewable); $data->setPaginationGetVar($this->pageVar); return $data; }
/** * @see PayPalDirectGateway::process() */ public function process($data) { $amount = $data['Amount']; $cents = round($amount - intval($amount), 2); switch ($cents) { case 0.0: $response = new RestfulService_Response($this->generateDummyResponse($data, 'Success')); break; case 0.01: $response = new RestfulService_Response(null, '500'); break; case 0.02: $response = new RestfulService_Response($this->generateDummyResponse($data, 'Failure')); default: $response = new RestfulService_Response($this->generateDummyResponse($data, 'Failure')); break; } if ($response->getStatusCode() != '200') { // Cannot connect to PayPal server return new PaymentGateway_Failure($response); } else { $responseArr = $this->parseResponse($response); if (!isset($responseArr['ACK'])) { return new PaymentGateway_Failure(); } else { switch ($responseArr['ACK']) { case self::SUCCESS_CODE: case self::SUCCESS_WARNING: return new PaymentGateway_Success(); break; case self::FAILURE_CODE: $errorList = $this->getErrors($response); return new PaymentGateway_Failure(null, null, $errorList); break; default: return new PaymentGateway_Failure(); break; } } } }