Connecting to remote servers (PHP / Thrift)

  • Time
  • Show
Clear All
new posts
  • chrisd
    • Mar 2004
    • 44

    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:
    = new THttpClient('''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)"121);

    //  <----- 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, 09:11 PM.
  • chrisd
    • Mar 2004
    • 44

    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 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.