PHP SDK Demystified – How file upload works

In the early version, file upload (e.g. uploading photo to Facebook) is not supported in the PHP SDK and we have to do it via curl.

But while the PHP SDK actually communicate with Facebook via curl, then why there is such a “limitation” in the early version of the PHP SDK?  The answer can be found by taking a look at how the PHP SDK formats the parameters that we specified.

In the makeRequest() function, we can see this is how the PHP SDK passes the parameter to curl:

 $opts = self::$CURL_OPTS;
 if ($this->useFileUploadSupport()) {
   $opts[CURLOPT_POSTFIELDS] = $params;
 } else {
   $opts[CURLOPT_POSTFIELDS] = http_build_query($params, null, '&');
 }
 $opts[CURLOPT_URL] = $url;

Before the file upload feature is supported in the PHK SDK, all the parameters we passed (including the file content) will be “pre-processed” the http_build_query function.  The http_build_query function will take the $params and generate a URL-encoded query string.

However, this is not how we should send the file content to Facebook!  For file upload, we need to send the file content via a multipart post instead of using a “single” URL-encoded query string.

This logic is enhanced now with the support of file upload in the PHP SDK and you can how the PHP SDK handles this by using the useFileUploadSupport() function.

This entry was posted in PHP SDK Demystified and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *