1) Download Package
silex.zip
2) Apache ModRewrite
the .htaccess contains the following code:RewriteEngine On
# Some hosts may require you to use the `RewriteBase` directive.
# If you need to use the `RewriteBase` directive, it should be the
# absolute physical path to the directory that contains this htaccess file.
#
# RewriteBase /
Header add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type"
Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
3) Test Functions
Objectives:
Get "/"
Get "/testget/1"
Post "/testpost" with form data key message (x-www-form-urlencoded)
Get "/testgetcontents" to call Get "/testget/"
Post "/testpostcontents" to call Get "/testpost/" with form data key message (x-www-form-urlencoded)
Codes:
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
$app = new Silex\Application();
$app->get('/', function(){
return new Symfony\Component\HttpFoundation\Response("Hello Silex");
});
$app->get("/testget/{id}", function($id){
return "testget - {$id}";
});
$app->post('/testpost', function (Request $request) {
$message = $request->get('message');
$reply = 'You have sent the following message:'.$message;
return new Response($reply, 201);
});
$app->get('/testgetcontents',function(){
$contents = file_get_contents('http://demo.razoph.com/silex/api/');
return $contents;
});
function file_post_contents($url, $data, $username = null, $password = null){
$postdata = http_build_query($data);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'content' => $postdata
)
);
if($username && $password)
{
$opts['http']['header'] .= ("Authorization: Basic " . base64_encode("$username:$password"));
// .= to append to the header array element
}
$context = stream_context_create($opts);
return file_get_contents($url, false, $context);
}
$app->get('/testpostcontents',function(){
$data = array("username" => "duser", "firstname" => "Demo", "message" => "testing", "email" => "example@example.com");
$contents = file_post_contents('http://demo.razoph.com/silex/api/testpost',$data);
return $contents;
});
$app->run();
REFERENCES:
https://silex.symfony.com/https://silex.symfony.com/doc/2.0/cookbook/index.html
https://www.gajotres.net/creating-a-basic-restful-api-with-silex/
http://www.robertprice.co.uk/robblog/posting-json-to-a-web-service-with-php/
https://stackoverflow.com/questions/11319520/php-posting-json-via-file-get-contents
https://symfony.com/doc/current/components/http_foundation.html
https://www.gajotres.net/creating-a-basic-restful-api-with-silex/
https://www.sitepoint.com/introduction-silex-symfony-micro-framework/
https://stackoverflow.com/questions/37346700/get-post-body-data-in-silex-restful-api
https://sleep-er.co.uk/blog/2013/Creating-a-simple-REST-application-with-Silex/
http://editor.swagger.io/
https://swagger.io/swagger-editor/
http://www.yaml.org/start.html
http://yaml-online-parser.appspot.com/
https://learnxinyminutes.com/docs/yaml/
https://stackoverflow.com/questions/1726802/what-is-the-difference-between-yaml-and-json-when-to-prefer-one-over-the-other
https://dzone.com/articles/working-angularjs-and-silex
https://gonzalo123.com/2015/02/02/handling-angularjs-post-requests-with-a-silex-backend/
https://gonzalo123.com/2013/06/03/working-with-angularjs-and-silex-as-resource-provider/
https://gonzalo123.com/2013/12/16/enabling-cors-in-a-restfull-silex-server-working-with-a-phonegapcordova-application/
https://stackoverflow.com/questions/643355/https-url-with-token-parameter-how-secure-is-it
https://stackoverflow.com/questions/36157059/angularjs-http-post-request-with-json-parameter-and-query-string
https://stackoverflow.com/questions/24545072/angularjs-http-post-send-data-as-json
http://tutlane.com/tutorial/angularjs/angularjs-http-post-method-http-post-with-parameters-example
https://github.com/vesparny/silex-simple-rest
https://github.com/shahroznawaz/Rest-API-in-Silex
https://github.com/mbezhanov/silex-rest-api-example
http://marinbezhanov.com/demo/vuejs-rest-client-example/#/diary
https://www.cloudways.com/blog/create-rest-api-silex/
No comments:
Post a Comment