Where is the tutorial "processor" generated? - php

I just started working with the github hosted build of apache thrift, I am basically interested in a java client and PHP server implementation, but for now i am using the php server and client only
All nice and easy i made my thrift file
namespace php mobiledata
struct sms
{
1: string from,
2: string to,
3: string smstext,
4: string smsdatetime,
5: string smsdirection
}
struct smsdetails
{
1: list<sms> smsdata
}
service mobiledataservice
{
void insertsmsdata (1: smsdetails smslist)
}
And I generated the gen-php folder, which has got Types.php and mobiledataservice.php
the basic sample that comes with the github for php as server shows a line of code
$handler = new CalculatorHandler();
$processor = new \tutorial\CalculatorProcessor($handler);
I can't find this class "CalculatorProcessor" and certainly I don't have a comparative class generated in my gen_php like mobiledataprocessor, and it baffles me as to how I would run my server in absence of processor.

The server code is generated by calling
thrift -r -gen php:server tutorial.thrift
Note the :server part after -gen php, this triggers the processor generation.
These are all PHP options available:
php (PHP):
inlined: Generate PHP inlined files
server: Generate PHP server stubs
oop: Generate PHP with object oriented subclasses
rest: Generate PHP REST processors

Related

Testing SLIM appication against API Specification with PHPUnit

I have an API, written in PHP with Slim (3, will be upgraded to 4 soon), I also have an extensive specification written in openapi3 YML format (I could convert to another format if necessary, but I think it would be the best to keep oas3). Currently I am testing all endpoint against this specification with Dredd. This is a Tool which goes through a API specification an, sends example data to the real API and checks if the result matches the spec. That works, but not very good. For a test run I have to wipe out and re-initialize the db with PHP and then run Dredd with npm. Plus since Dredd does not support all features of oas3 I have to do some conversion first.
Since I have some Unit-tests with PHPUnit anyway, I would love to run the other tests with PHPUnit as well (and get rid of the Node-stuff at all). I found http://opensource.byjg.com/php-swagger-test/ and https://github.com/Maks3w/SwaggerAssertions. Both of them provide this very feature, but I would have to write a separate function for every endpoint, containing sample data and so on - stuff which is already in the API spec. Any Idea how I could avoid this afford and just use my API spec as test defintion with PHPUnit or at least any PHP library?
Example from the specs:
/users:
get:
summary: get a list of users
description: get info about all registered users - in whole application or in a workspace.
parameters:
- in: header
name: AuthToken
schema:
$ref: '#/components/schemas/auth'
example:
at: 132token
- in: query
name: ws
description: id of a workspace to get users from. can be omitted for all users in system
required: false
examples:
a:
value: 0
b:
value: 1
With byjg php-swagger-test I would have to write something like this
public function usersA()
{
$request = new \ByJG\Swagger\SwaggerRequester();
$request
->withMethod('GET')
->withPath("/users")
->withHEader(blabla)
->withRequestBody(['ws'=>0]);
$this->assertRequest($request);
}
public function usersB()
{
$request = new \ByJG\Swagger\SwaggerRequester();
$request
->withMethod('GET')
->withPath("/users")
->withHEader(blabla)
->withRequestBody(['ws'=>1]);
$this->assertRequest($request);
}
Two tests (functions) containing only information which is already in the spec. Is there a better tool/way to run tests over all endpoints against the spec without writing all those?

python: read grpc proto request in interceptor

How can I read grpc proto request in interceptor with Python?
In PHP, I can read the $argument, it's what I need.
<?php
use Grpc\Interceptor;
class MyInterceptor extends Interceptor
{
public function interceptUnaryUnary($method,
$argument,
$deserialize,
array $metadata = [],
array $options = [],
$continuation)
{
// $argument is what I needto
return parent::interceptUnaryUnary($method, $argument, $deserialize, $metadata, $options, $continuation);
}
}
Unfortunately, gRPC Python doesn't have a complete server interceptor implemenataion that allow you to access request or servicer_context, but you may access to method string and invocation_metadata, for more detail check the history. If the semantic you want to achieve can be implemented in Python's metaclass or inheritance, please do so. If you would like to request for this feature, please post an issue in the GitHub repo grpc/grpc...
Here are some resources that might help you find the answer:
The documentation for Python interceptors
The design of Python interceptors
The example for Python interceptors
The example Python interceptor for server
I implemented a small grpc-interceptor package which provides a server interceptor that has access to the request and context. It uses the built in grpc.ServerInterceptor, and just simplifies the interface.
Here's a short snippet for how to use it:
class MyInterceptor(ServerInterceptor):
def intercept(self, method, request, context, method_name):
# Do fancy stuff here...
return method(request, context)
There are more complete examples in the documentation.
It has an MIT license, so you're free to use it however you like. If you'd rather not use the package, but want to see how it works, the code is here. It was adapted and simplified from an OpenTracing PR.

Creating a web service in PHP

I would like to create a web service in PHP which can be consumed by different consumers (Web page, Android device, iOS device).
I come from a Microsoft background so am confortable in how I would do it in C# etc. Ideally I would like to be able to provide a REST service which can send JSON.
Can you let me know how I can achieve this in PHP?
Thanks
Tariq
I developed a class that is the PHP native SoapServer class' REST equivalent.
You just include the RestServer.php file and then use it as follows.
class Hello
{
public static function sayHello($name)
{
return "Hello, " . $name;
}
}
$rest = new RestServer(Hello);
$rest->handle();
Then you can make calls from another language like this:
http://myserver.com/path/to/api?method=sayHello&name=World
(Note that it doesn't matter what order the params are provided in the query string. Also, the param key names as well as the method name are case-insensitive.)
Get it here.
I would suggest you go for Yii it is worth of learning. You can easily establish it in this.
Web Service. Yii provides CWebService and CWebServiceAction to simplify the work of implementing Web service in a Web application. Web service relies on SOAP as its foundation layer of the communication protocol stack.
Easiest way in PHP is to use GET/POST as data-in and echo as data-out.
Here's a sample:
<?php if(empty($_GET['method'])) die('no method specified');
switch($_GET['method']){
case 'add': {
if(empty($_GET['a']) || empty($_GET['b'])) die("Please provide two numbers. ");
if(!is_numeric($_GET['a']) || !is_numeric($_GET['b'])) die("Those aren't numbers, please provide numbers. ");
die(''.($_GET['a']+$_GET['b']));
break;
}
}
Save this as test.php and go to http://localhost/test.php?method=add&a=2&b=3 (or wherever your webserver is) and it should say 5.
PHP does have native support for a SOAP server ( The SoapServer class manual shows it) and I've found it pretty simple to use.
Creating a REST style API is pretty easy if you use a framework. I don't want to get into a debate about which framework is better but CakePHP also supports output as XML and I'm pretty sure others will as well.
If you're coming from a Microsoft background just be careful about thinking about "datasets". They are a very specific Microsoft thing and have been a curse of mine in the past. It's probably not going to be an issue for you, but you may want to just see the differences between Microsoft and open implementations.
And of course PHP has a native json_encode() function.
You can check out this nice RESTful server written for Codeigniter, RESTful server.
It does support XML, JSON, etc. responses, so I think this is your library.
There is even a nice tutorial for this on the Tutsplus network -
Working with RESTful Services in CodeIgniter
You can also try PHP REST Data Services https://github.com/chaturadilan/PHP-Data-Services
You can use any existing PHP framework like CodeIgniter or Symfony or CakePHP to build the webservices.
You can also use plain PHP like disscussed in this example

mysql and php with restler for webservice

Does anyone know how to code restler to work with php and mysql to produce something like the following:
I want to create a XML API Web Service and not sure where to start.
I want people to be able to query the database for information such as the following using a http request.
Example of Data
BrandName
Price
ShortDescription
SKU
Example Query
http://website.com/productxml?dep=1&Count=3&BrandName=Y&Price=Y
How would I go about writing such a script as I have searched the internet and cant find any examples and was wondering if you can help.
Thanks in advance
Roy
You could use Restler (http://luracast.com/products/restler/) and build a method
class YourClass {
public function productxml($dep, $Count, $BrandName, $Price) {
// your MySQL stuff
}
}
which handles your request.
See the examples (http://help.luracast.com/restler/examples/) how this can be done.
Hope this helps.
Greets.
You could use Restler #Restler Luracast.
The development has increased alot and its stable.
The fun part about this framework is that it supports multiple formats. All these formats can be added by just inserting a single line of code:
require_once '../../../vendor/restler.php';
use Luracast\Restler\Restler;
$r = new Restler();
$r->setSupportedFormats('JsonFormat', 'XmlFormat'); <---- Add format here
$r->addAPIClass('BMI');
$r->handle();
Also I would like to refer to my Luracast Restler template on bitbucket its public and its there for everybody to see.
I combined Restler with Doctrine so catching data from databases has never been easier. Its a raw version for now but I'll update it soon.
My version uses vagrant. Its a extension to virtualisation technology that makes development setup easy and fast. Once your application is ready you can deploy it to your server.
Link:Restler+Doctrine
1) Install virtualbox + vagrant
2) Clone my repository
3) Move to the cloned directory.
4) vagrant up
5) Enjoy and start programming your REST API in less than 10 minutes.

Generating WSDL when using PHP's native SOAP class?

I'm using the native SOAP class in PHP 5, having changed from NuSOAP as the native class is faster (and NuSOAP development seems to have ceased). However the PHP 5 SOAP lacks the ability to generate WSDL.
Has anyone experience of generating WSDL in PHP? If so, please recommend your preferred method.
Thanks.
Stuart,
If you or anyone else is looking for a solution to this problem here's what I did.
First get this script: http://www.phpclasses.org/browse/download/zip/package/3509/name/php2wsdl-2009-05-15.zip
Then look at its example files. After that I just sliced it the way I needed because I'm using codeigniter:
function wsdl(){
error_reporting(0);
require_once(APPPATH."/libraries/WSDLCreator.php"); //Path to the library
$test = new WSDLCreator("Webservice", $this->site."/wsdl");
//$test->includeMethodsDocumentation(false);
$test->addFile(APPPATH."/controllers/gds.php");
$test->addURLToClass("GDS", $this->site);
$test->ignoreMethod(array("GDS"=>"GDS"));
$test->ignoreMethod(array("GDS"=>"accessCheck"));
$test->createWSDL();
$test->printWSDL(true); // print with headers
}
That it, your all done.
Btw, I'm using SoapServer and SoapClient in php5+
You can try these options:
- https://code.google.com/p/php-wsdl-creator/ (GPLv3)
- https://github.com/piotrooo/wsdl-creator/ (GPLv3)
- http://www.phpclasses.org/package/3509-PHP-Generate-WSDL-from-PHP-classes-code.html (BSD like)
The first one might be your best option if the licence fits your project.
Generating a WSDL on the fly is not something that happens very often - it would tend to raise a few questions about the stability of your service!
Zend Studio can generate a WSDL from a PHP class, and there are a few other similar tools.
If you do need to generate the WSDL dynamically, take a look at Zend Framework library: Zend_Soap_AutoDiscover
Zend_Soap_AutoDiscover is a good alternative to NuSOAP. But, you can also create the WSDL file from scratch which can be very complicated and error prone. To ease this process, you can use an IDE to generate the WSDL file for your PHP functions and pass it as a parameter to your PHP SoapServer class. Check out the complete tutorial on How to generate wsdl for php native soap class

Categories