Mark Slee | 06af13d | 2007-03-21 06:50:52 +0000 | [diff] [blame] | 1 | Thrift PHP/Apache Integration |
| 2 | |
Bryan Duxbury | def30a6 | 2009-04-08 00:19:37 +0000 | [diff] [blame] | 3 | License |
| 4 | ======= |
| 5 | |
| 6 | Licensed to the Apache Software Foundation (ASF) under one |
| 7 | or more contributor license agreements. See the NOTICE file |
| 8 | distributed with this work for additional information |
| 9 | regarding copyright ownership. The ASF licenses this file |
| 10 | to you under the Apache License, Version 2.0 (the |
| 11 | "License"); you may not use this file except in compliance |
| 12 | with the License. You may obtain a copy of the License at |
| 13 | |
| 14 | http://www.apache.org/licenses/LICENSE-2.0 |
| 15 | |
| 16 | Unless required by applicable law or agreed to in writing, |
| 17 | software distributed under the License is distributed on an |
| 18 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 19 | KIND, either express or implied. See the License for the |
| 20 | specific language governing permissions and limitations |
| 21 | under the License. |
Mark Slee | 06af13d | 2007-03-21 06:50:52 +0000 | [diff] [blame] | 22 | |
| 23 | Building PHP Thrift Services with Apache |
| 24 | ======================================== |
| 25 | |
| 26 | Thrift can be embedded in the Apache webserver with PHP installed. Sample |
| 27 | code is provided below. Note that to make requests to this type of server |
| 28 | you must use a THttpClient transport. |
| 29 | |
| 30 | Sample Code |
| 31 | =========== |
| 32 | |
| 33 | <?php |
| 34 | |
Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame^] | 35 | namespace MyNamespace; |
| 36 | |
| 37 | /** |
| 38 | * Include path |
| 39 | */ |
| 40 | $THRIFT_ROOT = '/your/thrift/root/lib'; |
| 41 | |
| 42 | /** |
| 43 | * Init Autloader |
| 44 | */ |
| 45 | require_once $THRIFT_ROOT . '/Thrift/ClassLoader/ThriftClassLoader.php'; |
| 46 | |
| 47 | $loader = new ThriftClassLoader(); |
| 48 | $loader->registerNamespace('Thrift', $THRIFT_ROOT); |
| 49 | $loader->registerDefinition('Thrift', $THRIFT_ROOT . '/packages'); |
| 50 | $loader->register(); |
| 51 | |
| 52 | use Thrift\Transport\TPhpStream; |
| 53 | use Thrift\Protocol\TBinaryProtocol; |
| 54 | |
Mark Slee | 06af13d | 2007-03-21 06:50:52 +0000 | [diff] [blame] | 55 | /** |
| 56 | * Example of how to build a Thrift server in Apache/PHP |
Mark Slee | 06af13d | 2007-03-21 06:50:52 +0000 | [diff] [blame] | 57 | */ |
| 58 | |
Mark Slee | 06af13d | 2007-03-21 06:50:52 +0000 | [diff] [blame] | 59 | class ServiceHandler implements ServiceIf { |
| 60 | // Implement your interface and methods here |
| 61 | } |
| 62 | |
| 63 | header('Content-Type: application/x-thrift'); |
| 64 | |
| 65 | $handler = new ServiceHandler(); |
| 66 | $processor = new ServiceProcessor($handler); |
| 67 | |
| 68 | // Use the TPhpStream transport to read/write directly from HTTP |
| 69 | $transport = new TPhpStream(TPhpStream::MODE_R | TPhpStream::MODE_W); |
| 70 | $protocol = new TBinaryProtocol($transport); |
| 71 | |
| 72 | $transport->open(); |
| 73 | $processor->process($protocol, $protocol); |
| 74 | $transport->close(); |