2

我有一个当前运行的 cronjob,当达到某个阈值时,它会尝试打开与 Instagram 的连接并提取所有最近标记的与该标签匹配的照片。

问题是,当我尝试使用 curl 从命令行启动远程登录以“授权”我的应用程序时,Instagram 始终以网页响应

无法加载此页面。如果您在浏览器中禁用了 cookie,或者您在隐私模式下浏览,请尝试启用 cookie 或关闭隐私模式,然后重试您的操作。

这是我的卷曲脚本。

    $username = "<myusername>";
    $password = "<mypassword>";
    $useragent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31"; // Yes cause that's the way I roll
    $cookie="InstagramCookie.txt";

    $ch  = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/'.$cookie);
    curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/'.$cookie);
    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);

    $page = curl_exec($ch);

    // try to find the actual login form
    if (!preg_match('/<form method="POST" id="login-form" class="adjacent".*?<\/form>/is', $page, $form)) {
        throw Instagram_Manager('Failed to find log in form!');
    }

    $form = $form[0];

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

    $URL2 = $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['username'] = $username;
    $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);

    // set additional curl options using our previous options
    curl_setopt($ch, CURLOPT_URL, "https://instagram.com/".$url2);
    curl_setopt($ch, CURLOPT_REFERER, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

    $page = curl_exec($ch);

    file_put_contents("/tmp/page.txt", $page);

在这一点上,您的任何想法都会有所帮助。

4

1 回答 1

1

修复了一些基本错误后,尝试了您的代码并正常工作。

首先检查文件夹'/tmp'是否存在,并且其中的文件是可写和可读的。

改变

$URL2 = $action[1];

为了

$url2 = $action[1];

(可变为小写)

"https://instagram.com/".$url2

为了

$url.$url2

希望能帮助到你

于 2013-07-02T05:39:23.020 回答