How can I upload photos to album using Facebook Graph API

后端 未结 3 544
半阙折子戏
半阙折子戏 2020-12-11 10:37

Here is the code:

$file= \'bbbb.jpg\';
$data = array(
            basename($file) => \"@\".realpath($file),
            \"caption\" => \"Uploaded using         


        
3条回答
  •  天命终不由人
    2020-12-11 11:04

    The latest version of the Facebook PHP SDK wont work with the above examples without the following update to the code.

    class Facebook {
    ...
    *Line #539*
    protected function makeRequest($url, $params, $ch=null) {
    if (!$ch) {
      $ch = curl_init();
    }
    
    if( isset($params['doMultiPart']) ) {
        $doMultiPart= true;
        unset($params['doMultiPart']);
    } else {
        $doMultiPart= false;
    }
    
    $opts = self::$CURL_OPTS;
    $opts[CURLOPT_POSTFIELDS] = $doMultiPart ? $params : http_build_query($params, null, '&');
    ...
    

    Basically the problem is that the PHP SDK uses "curl_setopt_array" which if you pass it a url encoded string as the option value it will pass the data as application/x-www-form-urlencoded when what you really want is multipart/form-data; to do this we simply switch to passing in the array of options if we have a param of doMultiPart in the params array.

    This was a quick hack I put together to get something working, probably need to review the code to make sure it doesnt break anything else you are doing. Otherwise enjoy.

提交回复
热议问题