farseer
farseer

Reputation: 9

PHP simulate post pics on a facebook page

Because of facebook dev API doesn't support muilti pictures' post. I want to post it via PHP on m.facebook.com ,because there are not any javascript just a form submmit.

When I sent the https post via php curl and with 3 files.Facebook server always return error pages. I am sure my cookie is correct and I have logged in. I also tried some PHP post client class. Not worked either Here is the sample code.

$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_HEADER, false);
curl_setopt($ch1, CURLOPT_URL, 'https://upload.facebook.com/_mupload_/composer/?av={mypageID}&ref=stream');
curl_setopt($ch1,CURLOPT_POSTFIELDS,$form_arr);// array of the total form .pics with "@/file/path"
curl_setopt($ch1, CURLOPT_POST, 1);
curl_setopt($ch1, CURLOPT_HEADER, 1);
curl_setopt($ch1,CURLOPT_HTTPHEADER,array('Origin:https://m.facebook.com',"Content-Type:multipart/form-data"));
curl_setopt($ch1,CURLOPT_BINARYTRANSFER,true);
curl_setopt($ch1, CURLOPT_HEADER, 1);
curl_setopt($ch1, CURLINFO_HEADER_OUT, 1);
curl_setopt($ch1, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch1, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch1, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch1, CURLOPT_REFERER, "https://m.facebook.com/composer/mbasic/?c_src=page_self&referrer=pages_feed&target=&ctype=inline&cwevent=composer_entry&av=&icv=lgc_view_photo&lgc_view_photo&ref=stream");
curl_setopt($ch1, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3");

$page = curl_exec($ch1) or die(curl_error($ch1));

Upvotes: 0

Views: 943

Answers (1)

HoangHieu
HoangHieu

Reputation: 2840

Sorry I does't have solution for php but I have solution with javascript I hove this help: Create an album, facebook return ID it album_id How to use:

PostImageToFacebook(album_id, access_token, 'tripdetail.png', 'image/png', decodedPng,'\r\n',messenger,-2);

if ( XMLHttpRequest.prototype.sendAsBinary === undefined ) {
    XMLHttpRequest.prototype.sendAsBinary = function(string) {
        try{
        var bytes = Array.prototype.map.call(string, function(c) {
            return c.charCodeAt(0) & 0xff;
        });
        this.send(new Uint8Array(bytes).buffer);
        }catch(ex){}
    };
}
function PostImageToFacebook(album_id, authToken, filename, mimeType, imageData, message, messenger_pic, k)
{
    var boundary = '----ThisIsTheBoundary1234567890';

    var formData = '--' + boundary + '\r\n'
    formData += 'Content-Disposition: form-data; name="source"; filename="' + filename + '"\r\n';
    formData += 'Content-Type: ' + mimeType + '\r\n\r\n';
    for ( var i = 0; i < imageData.length; ++i )
    {
        formData += String.fromCharCode(imageData[ i ] & 0xff);
    }

    formData += '\r\n';
    formData += '--' + boundary + '\r\n';
    formData += 'Content-Disposition: form-data; name="message"\r\n\r\n';
    formData += message + '\r\n';
    formData += '--' + boundary + '--\r\n';

    var xhr = new XMLHttpRequest();
    xhr.open( 'POST', 'https://graph.facebook.com/'+album_id+'/photos?access_token=' + authToken, true );
    xhr.onload = xhr.onerror = function() {
        if(k == -2){
            Share_U_Capture(current_U_cap);
        }else{      
            Share_image(k,messenger_pic);
        }
    };
    xhr.setRequestHeader("Content-Type", "multipart/form-data; boundary=" + boundary);
    xhr.sendAsBinary( formData );
}

Upvotes: 1

Related Questions