phpgremlinazure-cosmosdb-gremlinapi

Can't connect custom script to cosmos db using brightzone gremlin-php


I'm trying to build an app around the brightzone gremlin php api for cosmos db (free tier). I can get the api to work through the console (with the provided app) but when I try to use my own custom functions to call the exact same methods available in the API class – practically copying the code, which worked through the console) – I get this...

Fatal error: Uncaught TypeError: fwrite(): Argument #1 ($stream) must be of type resource, null given {...}

on line 174 in vendor/brightzone/gremlin-php/src/Connection.php

I'm new to Gremlin in general, and have limited experience using APIs. Is this some limitation to the free tier, or have I completely misunderstood something?


edit

I decided to test the api with varying degrees of extra setup on my part...

  1. Running the PHP CLI script: connect.php (included in cosmos graph php getting started quickstart) from a browser through xampp gave no errors. I'm seeing a spike in requests in cosmos' dashboard when I do this, so I know it's hitting the database.

  2. The same test through a vhost alias also gave no errors, while hitting the database.

  3. Creating my own function that calls the class and methods results in the fwrite error above.

Initializing the object

This is the same on both the CLI version and my script.

<?php
require_once('define.php');
require_once('vendor/autoload.php');
use \Brightzone\GremlinDriver\Connection;
$db = new Connection([
    'host' => HOST,
    'username' => USER,
    'password' => PASS
    ,'port' => PORT

    // Required parameter
    ,'ssl' => TRUE
]);

code comparison

Here's the provided CLI script I'm trying to emulate...

function countVertices($db)
{
    $query = "g.V().count()";
    printf("\t%s\n\tQuery: %s\n", "Counting all the vertices.", $query);
    $result = $db->send($query);

    if($result)
    {
        printf("\tNumber of vertices in this graph: %s\n\n", $result[0]);
    }
}

This results in an instant readout of the printf commands meant for command line responses, with correct data from the result property of the object, showing it was initialized, and then functioned properly.

but

Here's how I'm trying to call the method...

$query = "g.V().count()";
$result = $db->send($query);
var_dump($result);

The object initializes fine, but calling the send method throws the fwrite error.


Seriously... what am I missing?


Solution

  • I failed to check the END of the CLI php script, which had a "try" catch statement running all the functions. If I had checked, I'd have seen that the open and close methods...

    $db->open();
    //<--whatever querying here-->
    $db->close();
    

    are necessary to get the gremlin driver connected.

    It's all working now with:

    <?php
    require_once('define.php'); //custom script defining credentials as constants
    require_once('vendor/autoload.php');
    use \Brightzone\GremlinDriver\Connection;
    
    error_reporting(E_ALL ^ E_WARNING);
    
    $db = new Connection([
        'host' => HOST,
        'username' => USER,
        'password' => PASS,
        'port' => '443'
    
        // Required parameter
        ,'ssl' => TRUE
    ]);
    
    $db->timeout = 0.5;
    
    $db->open();
    
    $query = "g.V().count()";
    
    var_dump($db->send($query));
    
    $db->close();
    ?>
    

    This prints the correct response.