我几乎完成了这段代码,但我遇到了一个障碍,我正在为 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();
任何帮助实现这一点都会很棒,谢谢
顺便说一句,我看到人们已经完成了此操作,但正在出售此代码,完成后我将免费发布:)