PHP模拟Facebook页面上的帖子图片

时间:2015-05-07 09:04:45

标签: php facebook curl

因为facebook dev API不支持muilti图片'帖子。 我想通过PHP在m.facebook.com上发布它,因为没有任何javascript只是一个表单submmit。

当我通过php curl和3个文件发送https帖子时.Facebook服务器总是返回错误页面。 我确定我的cookie是正确的,我已经登录了。 我也试过一些PHP post客户端类。也没用 这是示例代码。

$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));

1 个答案:

答案 0 :(得分:1)

抱歉,我没有 php 的解决方案,但我有 javascript 的解决方案 我提供了这个帮助: 创建相册,Facebook返回 ID album_id 使用方法:

  

PostImageToFacebook(album_id,access_token,'tripdetail.png','image / png',decodingPng,'\ 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 );
}