autopost to pinterest

时间:2013-04-14 01:02:49

标签: php curl pinterest

我几乎完成了这个代码,但是我遇到了麻烦,我正在写一个autopin脚本来pinterest它加载页面但不会提交:(我只需要一点帮助来完成这个脚本。

function login() {
    // Create Cookie File
    $cookiefile = './pinitcookie.txt';
    if (file_exists($cookiefile)) { unlink ($cookiefile); }
    //User and pass to Pinterest
    $email    = '<email>';
    $password = '<passowrd>';

    // initial login page which redirects to correct sign in page, sets some cookies
    $URL =  'https://pinterest.com/login';
    $ch  = curl_init();
    curl_setopt($ch, CURLOPT_URL, $URL);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 1);        
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    //Create the page for login
    $page = curl_exec($ch);
    preg_match('/^Set-Cookie:\s*([^;]*)/mi', $page, $m);
    //Get Pinterest CSRF token
    parse_str($m[1], $cookies);
    $token=$cookies['csrftoken'];

    // try to find the actual login form
    if (!preg_match('/<form id="AuthForm".*?<\/form>/is', $page, $form)) {
        die('Failed to find log in form!');
    }

    $form = $form[0];

    // find the action of the login form
    if (!preg_match('/action="([^"]+)"/i', $form, $action)) {
        die('Failed to find login form url');
    }

    $URL2 = "https://pinterest.com".$action[1]; // this is our new post url

    // find all hidden fields which we need to send with our login, this includes security tokens 
    $count = preg_match_all('/<input type="hidden"\s*name="([^"]*)"\s*value="([^"]*)"/i', $form, $hiddenFields);

    $postFields = array();

    // turn the hidden fields into an array
    for ($i = 0; $i < $count; ++$i) {
        $postFields[$hiddenFields[1][$i]] = $hiddenFields[2][$i];
    }

    // add our login values
    $postFields['email']    = $email;
    $postFields['csrfmiddlewaretoken']   = $token;
    $postFields['password'] = $password;

    $post = '';

    // convert to string, this won't work as an array, form will not accept multipart/form-data, only application/x-www-form-urlencoded
    foreach($postFields as $key => $value) {
        $post .= $key . '=' . urlencode($value) . '&';
    }

    $post = substr($post, 0, -1);

    curl_setopt($ch, CURLOPT_URL, $URL2);
    curl_setopt($ch, CURLOPT_REFERER, $URL);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    $page = curl_exec($ch); // make request

    if ($page === FALSE) {
    var_dump(curl_getinfo($ch));

    }
    post_to_pinterest($ch, $token, $cookiefile, $URL);
}


function post_to_pinterest($ch, $token, $cookiefile, $URL) {

#URL3 = "http://www.pinterest.com/pin/create/button/?url=" . rawurlencode(get_permalink($post)) . "&media=" . rawurlencode($url) . "&description=" . rawurlencode(get_the_title($post));
#$URL3 = "http://www.pinterest.com/pin/create/button/?url=http://www.biofects.com&media=http://www.biofects.com/wp-content/uploads/2013/01/Untitled-11.png&description=This is a test";
$URL3 = "http://pinterest.com/pin/create/button/?";
#echo $URL3;
    $posting = "board=test&url=http://www.domain.com&media=http://www.domain.com/wp-content/uploads/2013/01/Untitled-11.png&description=This is a test&csrfmiddlewaretoken=$token";
    $x="error";
        //curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile); 
    curl_setopt($ch, CURLOPT_URL, $URL3);
        //curl_setopt($ch, CURLOPT_REFERER, $URL);
        curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $posting);
        //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        //curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
    //curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    //curl_setopt($ch, CURLOPT_VERBOSE, true);
    $pagetopost = curl_exec($ch);// make request
        if($x != 'error' & trim($x) != ''){
            echo "Curl Try Error".$x;
        }

  //      preg_match('/^Set-Cookie:\s*([^;]*)/mi', $pagetopost, $m);
        //Get Pinterest CSRF token
   //     parse_str($m[1], $cookies);
   //     $token=$cookies['csrftoken'];

    curl_close($ch);


}

login();

任何有助于实现这一目标的帮助都会很棒,谢谢

BTW我看到人们已经完成了这项工作,但正在销售此代码,完成后我将免费发布:)

1 个答案:

答案 0 :(得分:0)

嘿大家我发布了wordpress插件。我已经测试了它并且它已经在3域上工作,如果你不使用wordpress你可以把代码并使它成为你自己的。你可以在这里找到插件http://wordpress.org/extend/plugins/lazy-pinner/