/** * Display a listing of the resource. * * @return Response */ public function index() { try { $statusCode = 200; $response = ['topics' => []]; // $topics = Topic::all(); //get all topics $topics = DB::table('topics')->where('status', '1')->get(); foreach ($topics as $topic) { $response['topics'][] = ['id' => $topic->id, 'name' => $topic->name, 'description' => $topic->description]; } } catch (Exception $e) { $statusCode = 400; } return Response::xml($response, $statusCode); }
public function xml() { Response::macro('xml', function (array $vars, $status = 200, array $header = [], $xml = null) { if (is_null($xml)) { $xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><response/>'); } foreach ($vars as $key => $value) { if (is_array($value)) { Response::xml($value, $status, $header, $xml->addChild($key)); } else { $xml->addChild($key, $value); } } if (empty($header)) { $header['Content-Type'] = 'application/xml'; } return Response::make($xml->asXML(), $status, $header); }); }
<?php class Response { /* *按XML方式输出通信数据 */ public static function xml() { //发送头信息,按xml的格式显示数据 header("Content-Type:text/xml;"); $xml = "<?xml version='1.0' encoding='UTF-8'?>"; $xml .= "<root>"; $xml .= "<code>200</code>"; $xml .= "<message>提交成功</message>"; $xml .= "<id>1</id>"; $xml .= "<user>hanfeng</user>"; $xml .= "</root>"; echo "{$xml}"; } } //调用xml方法 Response::xml();
public function email_availability(array $params = []) { if (empty($params)) { App::abort(404); } $uri_request = $this->config['server_url'] . '/v2/customers/email-availability?email=' . @$params['email']; $result = self::getCurl($uri_request); self::make_xml(); return Response::xml([], 200, [], $result); }
/** * Instantiate the appropriate Response document handler based on the * format * * @param Guzzle\Http|Message|Response $response The Guzzle Response object * @param string $object The name of the pardot obj * * @access protected * * @return HGG\Pardot\AbstractResponseHanler */ protected function getHandler($response, $object) { $handler = null; switch ($this->format) { case 'json': $this->rawResponse = $response->json(); $handler = new JsonResponseHandler($this->rawResponse, $object); break; case 'xml': $this->rawResponse = $response->xml(); $handler = new XmlResponseHandler($this->rawResponse, $object); break; } return $handler; }
public function yandexFeed() { $vars = array(); $lands = Land::where('sold', 0)->where('price_house', 0)->with('photo')->get(); $ya_feed = View::make(Helper::layout('yandex-feed'), compact('lands'))->render(); $this->make_xml(); return Response::xml($vars, 200, array(), $ya_feed); }
Route::post('datasource/import', 'ImportController@datasource'); Route::post('dataset/import', 'ImportController@dataset'); Route::post('variable/import', 'ImportController@variable'); Route::post('entity/import', 'ImportController@entity'); }); Response::macro('xml', function ($vars, $status = 200, array $header = [], $xml = null, $elName = 'el') { if (is_null($xml)) { $xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><response/>'); } foreach ($vars as $key => $value) { if (is_array($value)) { Response::xml($value, $status, $header, $xml->addChild($key), $elName); } else { if (is_object($value)) { $key = $elName; Response::xml($value, $status, $header, $xml->addChild($key), $elName); } else { $xml->addChild($key, htmlspecialchars($value)); } } } if (empty($header)) { $header['Content-Type'] = 'application/xml'; } return Response::make($xml->asXML(), $status, $header); }); Route::get('view', 'ViewController@index'); Route::get('view/{id}', ['as' => 'view', 'uses' => 'ViewController@show']); Route::get('data', 'DataController@index'); Route::get('data/config/{id}', 'ChartsController@config'); Route::get('data/dimensions', ['as' => 'dimensions', 'uses' => 'DataController@dimensions']);
/** * Returns the response in the configured format * @param Response $response * @param string $format * @return mixed $response */ private function responseFormat($response, $format) { if ($format == 'json') { return $response->json(); } else { if ($format == 'xml') { return $response->xml(); } } return $response; }
Route::get('/', function () { return view('welcome'); }); Response::macro('xml', function ($vars, $status = 200, array $header = array(), $rootElement = 'response', $xml = null) { if (is_object($vars) && $vars instanceof Illuminate\Support\Contracts\ArrayableInterface) { $vars = $vars->toArray(); } if (is_null($xml)) { $xml = new SimpleXMLElement('<' . $rootElement . '/>'); } foreach ($vars as $key => $value) { if (is_array($value)) { if (is_numeric($key)) { Response::xml($value, $status, $header, $rootElement, $xml->addChild(str_singular($xml->getName()))); } else { Response::xml($value, $status, $header, $rootElement, $xml->addChild($key)); } } else { $xml->addChild($key, $value); } } if (empty($header)) { $header['Content-Type'] = 'application/xml'; } return Response::make($xml->asXML(), $status, $header); }); // How to use // routes.php Route::get('api.{ext}', function () { $data = ['status' => 'OK']; $ext = \File::extension(\Request::url());