Connecting to remote servers (PHP / Thrift)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chrisd
    Member
    • Mar 2004
    • 44

    #1

    Connecting to remote servers (PHP / Thrift)

    Is anyone using the Thrift libraries -- or anything else calling out to another server from here ?

    I’m working with the EverNote SDK (PHP) and I have not been able to receive remote data. Everything works fine on both my local server and another server, but even a stripped down version of the sample app fails here.

    PHP Code:
    <?php
    $userStoreHttpClient = new THttpClient('sandbox.evernote.com', '443', "/edam/user", 'https');
    $userStoreProtocol = new TBinaryProtocol($userStoreHttpClient);
    $userStore = new UserStoreClient($userStoreProtocol, $userStoreProtocol);
    
    // do something simple and check the protocol version (returns true or false).
    $versionOK = $userStore->checkVersion("Evernote EDAMTest (PHP)", 1, 21);
    
    //  <----- it never returns here. But gets stuck reading in ttransport.php
    ?>
    I’m convinced it’s something with the environment, but what?
    Last edited by chrisd; 07-19-2012, 08:11 PM.
  • chrisd
    Member
    • Mar 2004
    • 44

    #2
    In case there's a second person on this planet trying to figure this out, THRIFT or otherwise, it appears to be an issue with PHP's stream_context_create and inserting HTTP headers.

    Specifically I needed to POST "Content-Type: application/x-thrift" to the remote server, and although I was inserting the headers properly, they were being stripped and/or overridden on the way out.

    There's a post at http://stackoverflow.com/a/4249933 that pushed my in the right direction, and as a workaround I'm now using:
    PHP Code:
    ini_set("user_agent", "PHP/THttpClient\r\nAccept: application/x-thrift\r\nContent-Type: application/x-thrift"); 
    
    Appending to the user-agent gets us the rest of the headers as a bonus (if not a bit ugly).

    FWIW, here's THRIFT'S implementation (from THttpClient.php) that was failing:
    PHP Code:
        $headers = array('Host: '.$host,
                         'Accept: application/x-thrift',
                         'User-Agent: PHP/THttpClient',
                         'Content-Type: application/x-thrift',
                         'Content-Length: '.strlen($this->buf_));
    
        $options = array('method' => 'POST', 'protocol_version' => 1.1,
                         'header' => implode("\r\n", $headers),
                         'max_redirects' => 1,
                         'content' => $this->buf_);
           $this->buf_ = '';
    
        $contextid = stream_context_create(array('http' => $options));
        $this->handle_ = @fopen($this->scheme_.'://'.$host.$this->uri_, 'r', false, $contextid); 
    
    I'm still completely baffed as to why this is happening, but at least now we can deal with it.

    Comment

    Working...