I am just getting started with graph databases. I would like to talk to them in a PHP application. In particular, I am looking at OrientDB, mainly because of its licensing benefits and features over neo4j.
What is the recommended way to interact with OrientDB within PHP? Is there a generalized framework like tinkerpop for the PHP world?
I would like to query my graphs using mainly gremlin and a bit of OrientDB's extended SQL (only if necessary).
I know there are 2 php connectors: Orient and OrientDB-PHP, but have not tried them yet. Has anyone had any experiences with them? What are the pros and cons? I would of course prefer something like tinkerpop for PHP if it exists, but if it doesn't a library geared towards OrientDB is fine too.
I'm Alex, I actually wrote the biggest part of the codebase for Orient (the php library).
In PHP, currently, there is no thing like tinkerpop but I think you should be able to kickstart with Orient. Using it its extremely simple:
<?php
namespace Congow\Orient;
use Congow\Orient\Binding\HttpBinding;
use Congow\Orient\Binding\BindingParameters;
require __DIR__.'/../autoload.php';
$parameters = BindingParameters::create('http://admin:admin#127.0.0.1:2480/friends');
$binding = new HttpBinding($parameters);
$response = $binding->query('select from friends where any() traverse(0,1) ( #rid = #5:3 ) and #rid <> #5:3');
$friends = $response->getResult();
foreach ($friends as $friend) {
echo $friend->name, "\n";
}
Instead of writing the query from scratch you can also use the query builder:
$query = new Select(array('myClass'));
$query->orderBy("name ASC", false);
echo $query->getRaw() // SELECT FROM myClass ORDER BY name ASC
You could have a look at the tests of the library, or at the mini-samples in the example directory.
We are slowly keep going on with the development, so you find a few resources here: http://odino.org/blog/categories/orientdb/
Cheers,
I know It's been a while but I wanted to point out that you can use the Tinkerpop stack in PHP via available drivers.
These will allow you a certain level of abstraction over the underlying database you choose -- thus allowing you to switch DBs when needed (Neo4j, OrientDB, Titan, etc.).
TinkerPop 3
You can use Gremlin server (new name for rexster) via the php driver available:
gremlin-php check this tutorial
Tinkerpop 2 (no longer maintained)
You can use the Rexster server from the stack via the php drivers available:
rexpro-php check this article
Doolittle
rexpro-php-driver (PHP ext.)
Related
PHP's Mongo driver lacks a renameCommand function. There is reference to do this through the admin database. But it seems more recent versions of the Mongo driver don't let you just "use" the admin database if do don't have login privileges on that database. So this method no longer works. I've also read this doesn't work in sharded environments although this isn't a concern for me currently.
The other suggestion people seem to have is to iterate through the "from" collection and insert into the "to" collection. With the proper WriteConcern (fire and forget) this could be fairly fast. But it still means pulling down each record over the network into the PHP process and then uploading it back over the network back into the database.
I ideally want a way to do it all server-side. Sort of like an INSERT INTO ... SELECT ... in SQL. This way it is fast, network efficient and a low load on PHP.
I have just tested this, it works as designed ( http://docs.mongodb.org/manual/reference/command/renameCollection/ ):
$mongo->admin->command(array('renameCollection'=>'ns.user','to'=>'ns.e'));
That is how you rename an unsharded collection. One problem with MR is that it will change the shape of the output from the original collection. As such it is not very good at copying a collection. You would be better off copying it manually if your collection is sharded.
As an added note I upgraded to 1.4.2 (which for some reason comes out from the pecl channel into phpinfo() as 1.4.3dev :S) and it still works.
Updates:
Removed my old map/reduce method since I found out (and Sammaye pointed out) that this changes the structure
Made my exec version secondary since I found out how to do it with renameCollection.
I believe I have found a solution. It appears some versions of the PHP driver will auth against the admin database even though it doesn't need to. But there is a workaround where the authSource connection param is used to change this behavior so it doesn't auth against the admin database but instead the database of your choice. So now my renameCollection function is just a wrapper around the renameCollection command again.
The key is to add authSource when connecting. In the below code $_ENV['MONGO_URI'] holds my connection string and default_database_name() returns the name of the database I want to auth against.
$class = 'MongoClient';
if( !class_exists($class) ) $class = 'Mongo';
$db_server = new $class($_ENV['MONGO_URI'].'?authSource='.default_database_name());
Here is my older version that used eval which should also work although some environments don't allow you to eval (MongoLab gives you a crippled setup unless you have a dedicated system). But if you are running in a sharded environment this seems like a reasonable solution.
function renameCollection($old_name, $new_name) {
db()->$new_name->drop();
$copy = "function() {db.$old_name.find().forEach(function(d) {db.$new_name.insert(d)})}";
db()->execute($copy);
db()->$old_name->drop();
}
you can use this. "dropTarget" flag is true then delete exist database.
$mongo = new MongoClient('_MONGODB_HOST_URL_');
$query = array("renameCollection" => "Database.OldName", "to" => "Database.NewName", "dropTarget" => "true");
$mongo->admin->command($query);
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
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.
Despite its inadvisability, using PHP's shell commands to interact with non-php system commands remains a common way of quickly achieving certain results in web applications.
Has anyone abstracted out the common use cases into a class library (something in Zend maybe?) that offers a more sane/common way of handling this? Every time I encounter (or have to produce) this kind of code it's a bunch of procedural spaghetti, copy-pasted over and over again. I was wondering if (hoping that) the PHP community had come up with a better way of handling using command line applications in your web/php applications.
Executing commandline applications is nothing dirty. In fact, it's the Unix way. And most mostly it's saner than trying to reimplement e.g. ImageMagick in pure PHP code. (Due to the disparity of its cmdline args, imagemagick is a bad example case if you look for a nice exec() abstraction.)
There isn't much wrapping up you can do. At best you can summarize in-/output to your external binary in a method:
function exec($args) {
$args = implode(" ", array_map("escapeshellcmd", func_get_args()));
$opts = $this->opts();
return `{$this->bin} {$args} {$opts}`;
}
So you just call ->exec("-o", "$file") where needed. Your code can only be gneralized further with specialized exec submethods, if the particular cmdline app has an inherent system in its --argument naming scheme.
Depending on your actual use case, you might be able to stash a few standard options away. I did this for pspell, where you have an almost 1:1 relationship of option names to --cmdline=args:
function opts() {
$map = array(
"--ignore" => $this->ignore,
"--verbose" => $this->verbose,
"--dir={$this->dir}" => isset($this->dir),
);
return implode(" ", array_keys(array_intersect($map, array(1=>1))));
}
A very generic abstraction class for exec/popen (for a wide range of cmdline programs) probably doesn't exist.
I'd love to see some code here rather than enjoy some sourse outside. =)
A solution could be to use the Zend_Service_Amazon_S3 component that's provided in Zend Framework -- if it's like many other components of ZF, it might be possible to use it outside of the framework, without having to do too much work to "extract" it.
I've never used it, but there are some examples of code on that manual page, and it doesn't seem too hard to use (quoting) :
require_once 'Zend/Service/Amazon/S3.php';
$s3 = new Zend_Service_Amazon_S3($my_aws_key, $my_aws_secret_key);
$s3->createBucket("my-own-bucket");
$s3->putObject("my-own-bucket/myobject", "somedata");
echo $s3->getObject("my-own-bucket/myobject");
(There are a couple of other examples I won't copy-paste)
An advantage of using a Zend Framework component is that ZF has (mostly) a good reputation, with code that's tested, maintained, supported, ...
Another solution could be to use this Amazon S3 PHP class ; I've never used it either, though...
<?php
require_once("sdk.class.php");
$s3 = new AmazonS3();
$bkt = (strtolower)$s3->key . "-bucket00";
$res = $s3->create_bucket($bkt, AmazonS3::REGION_US_E1);
?>