Loading ...
Sorry, an error occurred while loading the content.

PHP Download Attachment

Expand Messages
  • alex.mason77
    Hi I m trying to use php to download an attatchment ( I do have a premium account). Heres my code so far: $folder= $_GET[ fid ]; $messageId= $_GET[ mid ];
    Message 1 of 8 , Nov 27, 2007
    View Source
    • 0 Attachment
      Hi

      I'm trying to use php to download an attatchment ( I do have a premium account). Heres my code so far:


      $folder= $_GET["fid"];
      $messageId= $_GET["mid"];
      $partId = $_GET["pid"];
      $wssid = $authObj->WSSID;
      //var_dump($_Post);

      $curl = curl_init();

      //$curl_setopt($curl, CURLOPT_URL, "http://mail.yahooapis.com/ya/download?wssid=YLsB9fxXmkJ&appid&appid=o6guX97IkY1dEGqXKMwVG4f2ghsHGEHboB3eWA--&fid=Inbox&mid=1_1494_APqwktkAAA9TR0gqggEh4RAHxwQ&pid=5");
      echo "working";
      curl_setopt($curl, CURLOPT_URL, '"http://mail.yahooapis.com/ya/download');
      curl_setopt($curl, CURLOPT_POST,1);
      curl_setopt($curl, CURLOPT_COOKIE, $authObj->cookie);
      curl_setopt($curl, CURLOPT_POSTFIELDS, '"wssid= '.  $wssid  .'&appid='.  $appid .'&fid='. $folder .'&mid=' . $messageId .'&pid='.$partId.'&redirectURL=http%3A%2F%2Fweb32302.mail.mud.yahoo.com%2Fdc%2Fvirusresults.html%3Ffrom%3Ddownload_response%26ui%3Dlargewindow%26YY%3D1147203770155%26"');
      //curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($curl, CURLOPT_VERBOSE, 1);
      $result = curl_exec($curl);
      However, when this is run nothing happens. no error messages, just a blank screen. What I was hoping for was the attatchment to just download.

      I'm brand new to PHP - only used it for 2 days so I don't really know what I'm doing...

      Does anyone know how to download an attachment with php that could help me with this?
      Am I even going about this in the right way?

      Thanks

      Alex

    • Ryan Kennedy
      Here s what I do in my own script:
      Message 2 of 8 , Nov 27, 2007
      View Source
      • 0 Attachment
        Here's what I do in my own script:

        <?php

        // Has all of the code necessary to turn an token into
        // cookies and a WSSID.
        require('auth.inc');

        // Holds the application ID and shared secret.
        require('config.inc');

        // Get the bits out of the URL: token, folder ID, message ID,
        // part ID and whether or not the attachment should be inlined
        // or if the "Save As" dialog should be presented.
        $token = $_GET['token'];
        $fid = $_GET['fid'];
        $mid = $_GET['mid'];
        $pid = $_GET['pid'];
        $inline = $_GET['inline'];

        // Load the BBAuth credentials. Implementation is in auth.inc.
        // APP_ID and SECRET both come from config.inc.
        $credentials = load_user_credentials($token, $APP_ID, $SECRET);

        // Set up the query string parameters for the call to the mail
        // web service.
        $queryParams = array(
        "wssid" => $credentials->wssid,
        "appid" => $APP_ID,
        "fid" => $fid,
        "mid" => $mid,
        "pid" => $pid,
        "inline" => $inline
        );

        // Create the service URL.
        $url = sprintf('http://mail.yahooapis.com/ya/download?%s',
        http_build_query($queryParams));

        // Set up the cURL connection.
        $ch = curl_init($url);

        // Wire up a header handler that will echo headers from the cURL
        // request back to the caller. This ensures that the Content-type
        // and other relevant headers are set properly.
        curl_setopt($ch, CURLOPT_HEADERFUNCTION, "headerHandler");

        // Set up the cookies.
        curl_setopt($ch, CURLOPT_COOKIE, $credentials->cookie);

        // Execute the request. headerHandler() will be called repeatedly
        // as HTTP headers are read by cURL. When those finish, curl_exec()
        // will dump out the file itself.
        curl_exec($ch);

        // HTTP header handler. Copies the header to the response sent to
        // the caller.
        function headerHandler($ch, $header) {
        if(preg_match("/^\S+:\ .*/", $header)) {
        header($header);
        }
        return strlen($header);
        }

        ?>

        Ryan Kennedy
        Yahoo! Mail Web Service
      • alex.mason77
        Hi, Thanks v much for your speedy reply. I am now getting an Invalid (missing) timestamp error no 2004 I replaced my code with what you wrote. I tried
        Message 3 of 8 , Nov 27, 2007
        View Source
        • 0 Attachment
          Hi,

          Thanks v much for your speedy reply.

          I am now getting an "Invalid (missing) timestamp" error no "2004"

          I replaced my code with what you wrote.

          I tried addding the timestamp to the url, but it made no difference.


          What am I doing wrong?
          I have used my "userCreds.php" file to successfully display a users
          folders and a list of messages, and to display a message.


          Thanks again for your help.

          Alex




          How it looks now:

          require_once("JSON.php");
          // Include the proper class file
          $v = phpversion();
          if ($v[0] == '4') {
          include("ybrowserauth.class.php4");
          } elseif ($v[0] == '5') {
          include("ybrowserauth.class.php5");
          } else {
          die('Error: could not find the bbauth PHP class file.');
          }

          include ("userCreds.php");

          // Get the bits out of the URL: token, folder ID, message ID,
          // part ID and whether or not the attachment should be inlined
          // or if the "Save As" dialog should be presented.

          $fid = $_GET['fid'];
          $mid = $_GET['mid'];
          $pid = $_GET['pid'];
          $inline = 0;




          // Create the service URL.
          $downloadUrl =
          '"http://mail.yahooapis.com/ya/download?wssid='.$authObj->WSSID
          .'&appid='. $appid .'&fid='. $fid .'&mid=' . $mid .'&pid='. $pid
          .'&inline=0"';


          echo $url;

          // Set up the cURL connection.
          $ch = curl_init($downloadUrl);

          // Wire up a header handler that will echo headers from the cURL
          // request back to the caller. This ensures that the Content-type
          // and other relevant headers are set properly.
          curl_setopt($ch, CURLOPT_HEADERFUNCTION, "headerHandler");

          // Set up the cookies.
          curl_setopt($ch, CURLOPT_COOKIE, $authObj->cookie);

          // Execute the request. headerHandler() will be called repeatedly
          // as HTTP headers are read by cURL. When those finish, curl_exec()
          // will dump out the file itself.
          curl_exec($ch);

          // HTTP header handler. Copies the header to the response sent to
          // the caller.
          function headerHandler($ch, $header) {
          if(preg_match("/^\S+:\ .*/", $header)) {
          header($header);
          }
          return strlen($header);
          }




          Contents of userCreds.php:

          <?php
          include("credholder.inc.php");



          if (empty($_GET["token"]))//if no token redirect
          {

          $URL="http://namek.computing.dundee.ac.uk/2007-ac31004/alexandermason/yahoo/signin.php";
          header ("Location: $URL");
          }


          $appid = "o6guX97IkY1dEGqXKMwVG4f2ghsHGEHboB3eWA--"; // my
          application ID, obtained at registration
          $token = $_GET["token"]; // the user's token, obtained at user login
          // the user's token,
          obtained at user login
          $ts = time(); // seconds since Jan
          1, 1970 GMT
          $secret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; // my shared secret,
          obtained at registration

          $sig = md5(
          "/WSLogin/V1/wspwtoken_login?appid=$appid&token=$token&ts=$ts" .
          "$secret" );
          $url =
          "https://api.login.yahoo.com/WSLogin/V1/wspwtoken_login?appid=$appid&token=$token";




          $creds = new credHolder($appid, $secret);

          if ($creds->ready != true)
          {
          $tempArray =array();
          //requestify_url( $url, $secret );
          $signed_url = requestify_url( $url, $secret );
          $tempArray = get_credentials($signed_url );

          $creds->wssid = $tempArray["WSSID"];
          $creds->cookie = $tempArray["COOKIE"];
          $creds->timeout = $tempArray["timeout"];
          $creds->ready - true;


          //return;

          }



          //echo '<b>WSSID value is:</b> '. $creds->wssid .'<br />';
          //echo '<b>Cookie value is:</b> '. $creds->cookie .'<br />';
          //echo '<b>Timeout value is:</b> '. $creds->timeout .'<br />';

          $authObj = new YahooMailJSONRPC($creds->appid, $creds->secret);
          $authObj->timeout = $creds->timeout;
          $authObj->token = $creds->token;
          $authObj->WSSID = $creds->wssid;
          $authObj->cookie = $creds->cookie;



          function requestify_url( $url, $secret )//insert appid, token,
          timestamp sig
          {
          $parts = parse_url( $url );

          $ts = time();
          $relative_uri = "";

          if ( isset( $parts["path"] ) )
          {
          $relative_uri .= $parts["path"];
          }
          if ( isset ( $parts["query" ] ) )
          {
          $relative_uri .= "?" . $parts["query"] . "&ts=$ts";
          }

          $sig = md5( $relative_uri . $secret );

          $signed_url = $parts["scheme"] . "://" . $parts["host"] .
          $relative_uri . "&sig=$sig";
          //echo 'signurl1'. $signed_url . '<br />';
          return $signed_url;
          }


          function get_credentials( $signed_url )
          {




          $buf = file_get_contents($signed_url);//get the contents of the xml file


          if (empty($buf))
          {
          print "nothing returned for some weird reason";
          }



          if ( preg_match( "/(Y=.*)/", $buf, $match_array ) == true ) {

          $COOKIE = $match_array[1];

          }
          if ( preg_match( "/<WSSID>(.+)<\/WSSID>/", $buf, $match_array ) ==
          true ) {
          $WSSID = $match_array[1];
          }
          if ( preg_match( "/<Timeout>(.+)<\/Timeout>/", $buf, $match_array )
          == true ) {
          $timeout = $match_array[1];
          }

          $rv = array();
          $rv["COOKIE"] = $COOKIE;
          $rv["WSSID"] = $WSSID;
          $rv["timeout"] = $timeout;
          return $rv;
          }
          ?>















          --- In ydn-mail@yahoogroups.com, "Ryan Kennedy" <rckenned@...> wrote:
          >
          > Here's what I do in my own script:
          >
          > <?php
          >
          > // Has all of the code necessary to turn an token into
          > // cookies and a WSSID.
          > require('auth.inc');
          >
          > // Holds the application ID and shared secret.
          > require('config.inc');
          >
          > // Get the bits out of the URL: token, folder ID, message ID,
          > // part ID and whether or not the attachment should be inlined
          > // or if the "Save As" dialog should be presented.
          > $token = $_GET['token'];
          > $fid = $_GET['fid'];
          > $mid = $_GET['mid'];
          > $pid = $_GET['pid'];
          > $inline = $_GET['inline'];
          >
          > // Load the BBAuth credentials. Implementation is in auth.inc.
          > // APP_ID and SECRET both come from config.inc.
          > $credentials = load_user_credentials($token, $APP_ID, $SECRET);
          >
          > // Set up the query string parameters for the call to the mail
          > // web service.
          > $queryParams = array(
          > "wssid" => $credentials->wssid,
          > "appid" => $APP_ID,
          > "fid" => $fid,
          > "mid" => $mid,
          > "pid" => $pid,
          > "inline" => $inline
          > );
          >
          > // Create the service URL.
          > $url = sprintf('http://mail.yahooapis.com/ya/download?%s',
          > http_build_query($queryParams));
          >
          > // Set up the cURL connection.
          > $ch = curl_init($url);
          >
          > // Wire up a header handler that will echo headers from the cURL
          > // request back to the caller. This ensures that the Content-type
          > // and other relevant headers are set properly.
          > curl_setopt($ch, CURLOPT_HEADERFUNCTION, "headerHandler");
          >
          > // Set up the cookies.
          > curl_setopt($ch, CURLOPT_COOKIE, $credentials->cookie);
          >
          > // Execute the request. headerHandler() will be called repeatedly
          > // as HTTP headers are read by cURL. When those finish, curl_exec()
          > // will dump out the file itself.
          > curl_exec($ch);
          >
          > // HTTP header handler. Copies the header to the response sent to
          > // the caller.
          > function headerHandler($ch, $header) {
          > if(preg_match("/^\S+:\ .*/", $header)) {
          > header($header);
          > }
          > return strlen($header);
          > }
          >
          > ?>
          >
          > Ryan Kennedy
          > Yahoo! Mail Web Service
          >
        • alex.mason77
          Please ignore my last message- I wasn t thinking. Sorry The error I am actually getting is a 502 - Cannot find server or DNS Error
          Message 4 of 8 , Nov 27, 2007
          View Source
          • 0 Attachment
            Please ignore my last message- I wasn't thinking. Sorry

            The error I am actually getting is a 502 - Cannot find server or DNS Error
          • Ryan Kennedy
            ... Error Where do you see that error? In the browser? In your apache error logs? Is there any context indicating what DNS hostname it cannot resolve? Ryan
            Message 5 of 8 , Nov 27, 2007
            View Source
            • 0 Attachment
              --- In ydn-mail@yahoogroups.com, "alex.mason77" <alex.mason77@...> wrote:
              >
              > Please ignore my last message- I wasn't thinking. Sorry
              >
              > The error I am actually getting is a 502 - Cannot find server or DNS
              Error

              Where do you see that error? In the browser? In your apache error
              logs? Is there any context indicating what DNS hostname it cannot resolve?

              Ryan Kennedy
              Yahoo! Mail Web Service
            • alex.mason77
              No, me being silly again. I only get that error if I dont send the cookie. I m pretty sure now that I m not getting the attachment because I m just not sending
              Message 6 of 8 , Nov 28, 2007
              View Source
              • 0 Attachment
                No, me being silly again.

                I only get that error if I dont send the cookie.

                I'm pretty sure now that I'm not getting the attachment because I'm
                just not sending the correct part ID.

                I'm using JSON to get the messages and then I make a link for the user
                to click on to download the message. Is this the right way to get a
                part ID? $results is the result of GetMessage.

                $i = 5;
                $wssid = $authObj->WSSID;


                while ($results->result->message[0]->part[$i]->filename)
                {

                //echo '<a href=http://mail.yahooapis.com/ya/download?appid='.
                $authObj->appid .'&WSSID=' . $authObj->WSSID
                .'&mid='.$messageId.'&fid='.$folder.'&pid='.$i.'">';
                echo '<a href="displayAttatchment.php?token='. $_GET["token"]
                .'&fid='. $folder .'&mid=' . $messageId .'&pid='.$i.'">' ;
                $atts = $results->result->message[0]->part[$i]->filename;
                echo $atts ;
                echo '</a>';
                echo '<br/>';

                $i++;
                }

                echo '<br/>';


                Alex

                --- In ydn-mail@yahoogroups.com, "Ryan Kennedy" <rckenned@...> wrote:
                >
                > --- In ydn-mail@yahoogroups.com, "alex.mason77" <alex.mason77@> wrote:
                > >
                > > Please ignore my last message- I wasn't thinking. Sorry
                > >
                > > The error I am actually getting is a 502 - Cannot find server or DNS
                > Error
                >
                > Where do you see that error? In the browser? In your apache error
                > logs? Is there any context indicating what DNS hostname it cannot
                resolve?
                >
                > Ryan Kennedy
                > Yahoo! Mail Web Service
                >
              • Ryan Kennedy
                ... Actually, you want to use partId instead of filename. The partId is uniquely identifying, whereas you may receive multiple attachments each with the same
                Message 7 of 8 , Nov 28, 2007
                View Source
                • 0 Attachment
                  --- In ydn-mail@yahoogroups.com, "alex.mason77" <alex.mason77@...> wrote:
                  > while ($results->result->message[0]->part[$i]->filename)

                  Actually, you want to use partId instead of filename. The partId is
                  uniquely identifying, whereas you may receive multiple attachments
                  each with the same filename.

                  Ryan Kennedy
                  Yahoo! Mail Web Service
                • alex.mason77
                  Thanks- thats it
                  Message 8 of 8 , Nov 30, 2007
                  View Source
                  • 0 Attachment
                    Thanks- thats it

                    :)


                    --- In ydn-mail@yahoogroups.com, "Ryan Kennedy" <rckenned@...> wrote:
                    >
                    > --- In ydn-mail@yahoogroups.com, "alex.mason77" <alex.mason77@> wrote:
                    > > while ($results->result->message[0]->part[$i]->filename)
                    >
                    > Actually, you want to use partId instead of filename. The partId is
                    > uniquely identifying, whereas you may receive multiple attachments
                    > each with the same filename.
                    >
                    > Ryan Kennedy
                    > Yahoo! Mail Web Service
                    >
                  Your message has been successfully submitted and would be delivered to recipients shortly.