0

Download Attachment Problems in PHP

I've been trying numerous approaches to get an attachment downloaded without success and could use some guidance and/or working examples.
I've read through various forum posts and still cannot get this Yahoo! API to work. I keep getting a 404 error.
I have the ListMessages and GetMessage working with the JSON endpoint of http://mail.yahooapis.com/ws/mail/v1.1/jsonrpc

My code is using the OAuth.php provided by Yahoo!

$token = "A%3D5r...qw--";
$token_secret = "74...4b";
$mid = "1_11...MY";
$consumer_key = "...";
$consumer_secret = "...";

$nonce = rand();
$timestamp = time();
$signmethod = "HMAC-SHA1";
$version = "1.0";
$method = "GET";

$url = "http://mail.yahooapis.com/ya/download/?mid=" . $mid . "&fid=Inbox&pid=2&clean=0&inline=0";

$signature = new OAuthSignatureMethod_HMAC_SHA1();
$request = new OAuthRequest('GET', $url, array(
'oauth_consumer_key'=>$consumer_key,
'oauth_nonce'=>$nonce,
'oauth_signature_method'=>$signmethod,
'oauth_timestamp'=>$timestamp,
'oauth_token'=>$token,
'oauth_version'=>$version));

$request->sign_request($signature, new OAuthConsumer($consumer_key, $consumer_secret), new OAuthToken($token, $token_secret));
$oauthURL = $request->to_url();

//$fh = fopen('/tmp/test2-'. $mid, 'w');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $oauthURL);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_FILE, $fh);
$header = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
//fclose($fh);



I keep getting a simple 404 error. Any ideas, suggestions, or working examples would be greatly appreciated.

John

by
14 Replies
  • I could still use help on this. I've moved on to other tasks but I will come back to this if I ever get an answer to this. It would be a nice way to incorporate Yahoo users into our product more.
    0
  • Can you please urlencode mid and try it again. Thanks.

    --R

    QUOTE (John H @ Jun 9 2011, 11:01 AM) <{POST_SNAPBACK}>
    I could still use help on this. I've moved on to other tasks but I will come back to this if I ever get an answer to this. It would be a nice way to incorporate Yahoo users into our product more.
    0
  • urlencoding the $mid did not change the value of the mid as it has no characters that need urlencoding and hence still leaves me with a 404 error.
    0
  • Does anybody have working example PHP code for a download attachment?
    0
  • Hi,

    I am giving an outline of a standard download request. I hope this helps.

    $endPointDownload = 'http://mail.yahooapis.com/ya/download';

    //Create your Download request query string.
    $queryString = $endPointDownload . "?";
    $queryString .= "mid=".urlencode($midRequest);
    $queryString .= "&fid=Inbox";
    $queryString .= "&pid=2";
    $queryString .= "&clean=0";
    $queryString .= "&output=xml";

    //if needed.
    $downloadheader = new stdclass(); // hold this for loading content-disposition, content-length and error if any.

    //OAuth header for download request
    $request_args = array(
    'oauth_nonce'=>mt_rand(),
    'oauth_timestamp'=>time(),
    'oauth_version'=>'1.0',
    'oauth_signature_method'=>'HMAC-SHA1',
    'oauth_consumer_key'=>$OAuthConsumerKey,
    'oauth_token'=>$oauth_token,
    'mid'=>$midRequest, // Make sure its the correct mid.
    'fid'=>'Inbox', // Folder id
    'pid'=>'2', // part id.
    'clean'=>'0',
    'output'=>'xml'
    );

    //Note its a GET
    $request = new OAuthRequest('GET', "$endPointDownload", $request_args);
    $request->sign_request($signature, new OAuthConsumer('', $OAuthConsumerSecret), new OAuthToken('', $oauth_token_secret));
    $oauthHeaderForDownloadJson = $request->to_header();

    //Do curl_exec
    $ch = curl_init($queryString);
    curl_setopt($ch,CURLOPT_HTTPHEADER,array($oauthHeaderForDownloadJson));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_HEADERFUNCTION, "readDownloadHeader");
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    $rawresponse = curl_exec($ch);
    var_dump($rawresponse);

    //Get downloaded data
    $response = new stdclass();
    $response->body = $rawresponse;
    $response->code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $response->contenttype = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
    $response->contentlength = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
    response->lasteffectiveurl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
    $response->contentdisposition = isset($downloadheader->contentdisposition)? $downloadheader->contentdisposition : "";
    $response->headercontentlength = isset($downloadheader->contentlength)? $downloadheader->contentlength : 0;
    $response->errormessage = isset($downloadheader->errormessage)? $downloadheader->errormessage : "";
    var_dump($response);

    function readHeaderInformation($ch, $header) {
    //read from header if needed - info like conten-tdisposition, content-length and error-message
    //$downloadheader->contentdisposition, $downloadheader->contentlength, $downloadheader->errormessage
    }

    --R

    QUOTE (John H @ Jun 20 2011, 06:54 AM) <{POST_SNAPBACK}>
    Does anybody have working example PHP code for a download attachment?
    0
  • Sadly... even your example results in 404 errors. Does it work for you?
    0
  • QUOTE (John H @ Jun 24 2011, 06:58 AM) <{POST_SNAPBACK}>
    Sadly... even your example results in 404 errors. Does it work for you?


    Yep, same here. I read somewhere that DownloadAttachment is only available for premium accounts?!?
    http://www.scribd.com/doc/49879097/26/DownloadAttachment

    Cheers,
    Slavik
    0
  • "I read somewhere that DownloadAttachment is only available for premium accounts?!?"
    No. Download and Upload both are supported for normal accounts.

    @John : Can you paste your entire code? The previous code you posted has differences with the one I had given. If it doesn't work for you put var_dumps/debug stmts in each step and tell me where it fails.

    Few corrections in your previous code
    1. Urlencode the mid
    2. Do to_header() instead of to_url().
    3. Make sure your value of pid,mid and fid are correct and add &clean and &output=xml also to the query args.

    Use the same curl code as below.
    $ch = curl_init($queryString);
    curl_setopt($ch,CURLOPT_HTTPHEADER,array($oauthHeaderForDownloadJson));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_HEADERFUNCTION, "readDownloadHeader");
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    $rawresponse = curl_exec($ch);

    --R

    QUOTE (slavik.markovich @ Jun 29 2011, 03:06 PM) <{POST_SNAPBACK}>
    Yep, same here. I read somewhere that DownloadAttachment is only available for premium accounts?!?
    http://www.scribd.com/doc/49879097/26/DownloadAttachment

    Cheers,
    Slavik
    0
  • In general, I had the most trouble spotting and fixing percent encoding related problems. Specifically:

     - ListMessages() does _not_ return message ID as a percent encoded string (even though folderID returned in the same call _is_ percent encoded), so you need to so that yourself (as Ram also mentioned)

     - If you are refreshing your OAuth access token, it _is_ returned percent encoded, so you need to make sure you don't encode it twice when you use it for API calls.

    I really wish the Yahoo Mail documentation had done a better job documenting which strings are returned percent encoded and which ones are not. This has been a huge source of problems and a time sink to spot/debug.

    Rajesh
    0
    • Aug 23, 2012
    Hello, i have tried the above sample code, but same problem.......
    whyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy....
    i need help.
    0
    • Aug 28, 2012
    Hello,
    http://developer.yahoo.com/mail/account_types.html 
    the link above shows that non-premium accounts can't access downloadattachments, uploadattachment.

    0
  • Non-premium accounts can access Upload and Download Attachment APIs. Thank you.
    0
  • Did the DownloadAttachment API still work?
    I use Ram's sample code, but it keeps geting 404.
    Did I miss something?

    Header:
    Authorization: OAuth oauth_nonce="1770537812",oauth_timestamp="1348664589",oauth_version="1.0",oauth_signature_method="HMAC-SHA1",oauth_consumer_key="[key]",oauth_token="[token]",oauth_signature="[sig]"

    Get:
    http://mail.yahooapis.com/ya/download?mid=2_0_0_1_641048_AMH0i2IAALg8UGLnLgEqjBjGo7E&fid=Inbox&pid=2&clean=0&output=xml

    mid and authorization token are correct because I can call GetMessage to get the correct message.

    Thanks!

    QUOTE(ram @ 4 Sep 2012 3:45 PM)
    Non-premium accounts can access Upload and Download Attachment APIs. Thank you.
    0

  • Did the DownloadAttachment API still work?
    Yes. It works.

    Are you resigning the request for download endpoint? 
    What does the request and response payload look like? 

    --Ram

    QUOTE(Easilydo @ 26 Sep 2012 7:08 AM)
    Did the DownloadAttachment API still work?
    I use Ram's sample code, but it keeps geting 404.
    Did I miss something?

    Header:
    Authorization: OAuth oauth_nonce="1770537812",oauth_timestamp="1348664589",oauth_version="1.0",oauth_signature_method="HMAC-SHA1",oauth_consumer_key="[key]",oauth_token="[token]",oauth_signature="[sig]"

    Get:
    http://mail.yahooapis.com/ya/download?mid=2_0_0_1_641048_AMH0i2IAALg8UGLnLgEqjBjGo7E&fid=Inbox&pid=2&clean=0&output=xml

    mid and authorization token are correct because I can call GetMessage to get the correct message.

    Thanks!

    QUOTE(ram @ 4 Sep 2012 3:45 PM)
    Non-premium accounts can access Upload and Download Attachment APIs. Thank you.
    0

Recent Posts

in Yahoo! Mail Web Services API