We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 31471
    • 206 Posts
    I wish to create a snippet that inserts events in my Google Calendar using the Zend Gdata API. This can be downloaded separated from the framework from http://framework.zend.com/download/gdata
    My trial/evaluation code is working flawlessly on my test environment, running from the webroot as a php-file.
    However, the same code inside a snippet doesn’t do.

    Here’s my Add2GCal.php file and snippet code:
    (The Zend folder containing the Loader.php file and others must be either in the php-path or in the webroot.)
    <?php
    echo "Starting...<br />";
    require_once 'Zend/Loader.php';
    Zend_Loader::loadClass('Zend_Gdata');
    Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
    Zend_Loader::loadClass('Zend_Gdata_Calendar');
    Zend_Loader::loadClass('Zend_Http_Client');
    
    // Parameters for ClientAuth authentication
    $service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
    $user = "[email protected]";
    $pass = "mypassword";
    
    echo "Creating HTTP client... ";
    
    // Create an authenticated HTTP client
    $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
    echo "OK<br />";
    
    // Create an instance of the Calendar service
    $service = new Zend_Gdata_Calendar($client);
    
    echo"Calendar service OK...<br />";
    
    // Create a new entry using the calendar service's magic factory method
    $event= $service->newEventEntry();
    
    // Populate the event with the desired information
    // Note that each attribute is crated as an instance of a matching class
    $event->title = $service->newTitle("My Event");
    $event->where = array($service->newWhere("Mountain View, California"));
    $event->content = $service->newContent(" This is my awesome event. RSVP required.");
    
    // Set the date using RFC 3339 format.
    $startDate = "2008-07-21";
    $startTime = "14:00";
    $endDate = "2008-07-26";
    $endTime = "16:00";
    $tzOffset = "0";
    
    $when = $service->newWhen();
    $when->startTime = "{$startDate}T{$startTime}:00.000{$tzOffset}:00";
    $when->endTime = "{$endDate}T{$endTime}:00.000{$tzOffset}:00";
    $event->when = array($when);
    
    echo "Writing... ";
    
    // Upload the event to the calendar server
    // A copy of the event as it is recorded on the server is returned
    $createdEvent = $service->insertEvent($event);
    
    echo "done... ";
    if ($createdEvent)
    echo "OK";
    else 
    echo "notOK";
    ?>


    Running this code directly from the webroot gives the right results both in the calendar and onscreen:

    [tt]Starting...
    Creating HTTP client... OK
    Calendar service OK
    Writing... done... OK[/tt]

    Running it within MODx from a snippet gives me the following error:

    [tt]Starting...
    Creating HTTP client...

    « MODx Parse Error »
    MODx encountered the following error while attempting to parse the requested resource:
    « PHP Parse Error »

    PHP error debug
    Error: fopen(Zend/Validate/Hostname/Com.php) [function.fopen]: failed to open stream: No such file or directory
    Error type/ Nr.: Warning - 2
    File: K:\xampp\php\Zend\Zend\Loader.php
    Line: 160
    Line 160 source: if (!$fh = @fopen($filename, ’r’, true)) { [/tt]

    Which means to me that it stucked in creating the HTTP client. Of course there’s no Com.php file in the whole Zend framework. I suppose the MODx parser is fooling Zend somehow...
    Line 160 in the Loader.php is part of a function which looks like this:
    //Returns TRUE if the $filename is readable, or FALSE otherwise. This function uses the PHP include_path, where PHP's is_readable() does not.
    public static function isReadable($filename)
        {
            if (!$fh = @fopen($filename, 'r', true)) {
                return false;
            }
    
            return true;
        }


    Can anyone help, please?
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      The parser runs all php code through the eval() function. It is possible that you need to declare some values as global in your snippet.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 31471
        • 206 Posts
        Thank you Sottwell, but these variables are in the scope of this code at the moment.
        Later I want to pass parameters to it of course.
        Anyway I tried to define all vars as global at the beginning (I’m not a php-master;) but the error persists.
          • 31471
          • 206 Posts
          I’ve just tested it in a real server environment with Apache etc. and it goes well!
          It’s my local XAMPP+MODx only not doing the job sad
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            There are some features of PHP that are either not available or behave differently on windows. I’m not a Windows user so I’m really not familiar with the differences, I just know that they exist.
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org
              • 31471
              • 206 Posts
              Yes, Susan. Now I also suppose so.
              I installed a WampServer2.0 on my local and exactly the same issue: good from webroot, bad from MODx-snippet.
              Just to remind: on a remote server it is OK from MODx snippet huh

              I should have to make a deep-dive into linux on a virtual machine...
                • 10487 MODX Staff
                • 1,535 Posts
                This is because MODx changes the error reporting levels which is overriding the error suppression on that line (signified by the ’@’ symbol)

                Now, it appears that the Zend Loader.php has special handling capabilities for some specific TLDs (although not for the .com TLD - which I assume is also maybe a default if no TLD exists e.g. localhost) So, it attempts to include a file based on the TLD and the error suppression should prevent an error being thrown if no file is found.

                Now, why they couldn’t use a simple file_exists() check is beyond me ...

                Back to the problem, what is the TLD on your remote Linux server and does a file exist in Zend/Validate/Hostname/ for it? If so, then that’s the answer as to why it works on the remote server and not your local install. Otherwise, maybe there’s some funky differences in how the errors get handled on the PHP side of affairs between the two servers.

                To try and work around the issue, you could try setting the error reporting to only report fatal errors around the code that creates the HTTP client in your snippet. Something like:
                error_reporting(E_ERROR);

                And, then set it back to the MODx error reporting after the HTTP client is created:
                error_reporting(E_ALL & ~E_NOTICE);

                Completely untested but sounds like it should work tongue More on error reporting here: http://us.php.net/manual/en/intro.errorfunc.php

                Hope that makes sense and helps,
                Garry
                  Garry Nutting
                  Senior Developer
                  MODX, LLC

                  Email: [email protected]
                  Twitter: @garryn
                  Web: modx.com
                  • 31471
                  • 206 Posts
                  I really appreciate your feedback Susan and Garry, but the answer is at a different place...

                  After installing both 0961p2 and 0962rc1 on both local and remote servers, it’s clear now that the latter version is OK with Zend Gdata while the other is not.
                  Only the new version does the job right. I’m curious why, of course!

                  So if anyone wants to poke Google, update to 0962rc1 at least! Actually it is downloadable from here: http://modxcms.com/beta.html

                  Maybe the funky guys at MODx headquarters can tell the difference wink