Overview

Packages

  • None
  • Service
    • Facebook

Classes

  • Horde_Service_Facebook_Auth
  • Horde_Service_Facebook_Base
  • Horde_Service_Facebook_BatchRequest
  • Horde_Service_Facebook_ErrorCodes
  • Horde_Service_Facebook_Events
  • Horde_Service_Facebook_Exception
  • Horde_Service_Facebook_Fql
  • Horde_Service_Facebook_Friends
  • Horde_Service_Facebook_Groups
  • Horde_Service_Facebook_Links
  • Horde_Service_Facebook_Notes
  • Horde_Service_Facebook_Notifications
  • Horde_Service_Facebook_Photos
  • Horde_Service_Facebook_Request
  • Horde_Service_Facebook_Streams
  • Horde_Service_Facebook_Translation
  • Horde_Service_Facebook_UploadRequest
  • Horde_Service_Facebook_Users
  • Horde_Service_Facebook_Videos
  • Overview
  • Package
  • Class
  • Tree
 1: <?php
 2: /**
 3:  * Upload Requests
 4:  *
 5:  * Copyright 2009-2012 Horde LLC (http://www.horde.org/)
 6:  *
 7:  * @author Michael J. Rubinsky <mrubinsk@horde.org>
 8:  * @category Horde
 9:  * @package Service_Facebook
10:  */
11: class Horde_Service_Facebook_UploadRequest extends Horde_Service_Facebook_Request
12: {
13:     /**
14:      * Filename to upload
15:      *
16:      * @var string
17:      */
18:     protected $_filename;
19: 
20:     /**
21:      * Const'r
22:      *
23:      * @param Horde_Service_Facebook $facebook
24:      * @param string                 $method
25:      * @param string                 $file
26:      * @param array                  $params
27:      */
28:     public function __construct(Horde_Service_Facebook $facebook, $method,
29:                                 $file, array $params = array())
30:     {
31:         parent::__construct($facebook, $method, $params);
32:         $this->_filename = $file;
33:     }
34: 
35:     /**
36:      * Run the request
37:      *
38:      * @return mixed
39:      */
40:     public function run()
41:     {
42:         // Ensure we ask for JSON
43:         $this->_params['format'] = 'json';
44:         $result = $this->_multipartHttpTransaction();
45:         return $result;
46:     }
47: 
48:     /**
49:      * Execute a RFC1867/RFC1341 Multipart Http Transaction.
50:      *
51:      * @todo Use Horde_Mime
52:      *
53:      * @throws Horde_Service_Facebook_Exception
54:      *
55:      * @return string
56:      */
57:     private function _multipartHttpTransaction()
58:     {
59:         // the format of this message is specified in RFC1867/RFC1341.
60:         // we add twenty pseudo-random digits to the end of the boundary string.
61:         $boundary = '--------------------------FbMuLtIpArT' .
62:                     sprintf("%010d", mt_rand()) .
63:                     sprintf("%010d", mt_rand());
64:         $content_type = 'multipart/form-data; boundary=' . $boundary;
65:         // within the message, we prepend two extra hyphens.
66:         $delimiter = '--' . $boundary;
67:         $close_delimiter = $delimiter . '--';
68:         $content_lines = array();
69:         $this->_finalizeParams($this->_method, $this->_params);
70:         foreach ($this->_params as $key => &$val) {
71:             $content_lines[] = $delimiter;
72:             $content_lines[] = 'Content-Disposition: form-data; name="' . $key . '"';
73:             $content_lines[] = '';
74:             $content_lines[] = $val;
75:         }
76: 
77:         // now add the file data
78:         $content_lines[] = $delimiter;
79:         $content_lines[] = 'Content-Disposition: form-data; filename="' . $this->_filename . '"';
80:         $content_lines[] = 'Content-Type: application/octet-stream';
81:         $content_lines[] = '';
82:         $content_lines[] = file_get_contents($this->_filename);
83:         $content_lines[] = $close_delimiter;
84:         $content_lines[] = '';
85:         $content = implode("\r\n", $content_lines);
86:         try {
87:             $result = $this->_http->request('POST',
88:                                             Horde_Service_Facebook::REST_SERVER_ADDR,
89:                                             $content,
90:                                             array('Content-Type' => $content_type,
91:                                                   'Content-Length' => strlen($content)));
92:         } catch (Exception $e) {
93:             throw new Horde_Service_Facebook_Exception(sprintf(Horde_Service_Facebook_Translation::t("Upload failed: %s"), $e->getMessage()));
94:         }
95: 
96:         return $result->getBody();
97:     }
98: 
99: }
API documentation generated by ApiGen