用PHP发布请求

用户名

我正在尝试进行登录php发布,但其发布不正确...我需要将以下内容发送到Curl,但它只是不发送

//Set the post parameters 
curl_setopt($ch, CURLOPT_POSTFIELDS, 'j_username='.$username.'&j_password='.$password.'&tk_trp ='.$tk_trp);

是我当前正在尝试使用的东西,只是没有服用,有人发现我做错了吗?任何建议将不胜感激!

我已经坚持了好一阵子了,但对于我来说,只是没有在一起,所以我想伸出手:)

  <?php
    $token = GetStringBetween(getURL("tokenlocatonurl"),"start'", "'");
    ini_set('display_errors', 1);
    error_reporting(E_ALL ^ E_NOTICE);
    $username = 'myuser';
    $password = 'mypass';
    $tk_trp = '$token';
    $loginUrl = 'lognurlhere';

        function getURL($u){
            $u = file_get_contents("http://{$u}");
            return $u != false ? $u : "";
        }
        function GetStringBetween($string, $start, $finish){
            $string = " ".$string;
            $position = strpos($string, $start);
            if ($position == 0) return "";
            $position += strlen($start);
            $length = strpos($string, $finish, $position) - $position;
            return substr($string, $position, $length);
        }

    //init curl
    $ch = curl_init();

    //Set the URL to work with
    curl_setopt($ch, CURLOPT_URL, $loginUrl);

    // ENABLE HTTP POST
    curl_setopt($ch, CURLOPT_POST, 1);

    //Set the post parameters  curl_setopt($ch, CURLOPT_POSTFIELDS, 'j_username='.$username'&j_password='.$password'&tk_trp='.$tk_trp');

    //Handle cookies for the login
    curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');

    //Setting CURLOPT_RETURNTRANSFER variable to 1 will force cURL
    //not to print out the results of its query.
    //Instead, it will return the results as a string return value
    //from curl_exec() instead of the usual true/false.
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    //execute the request (the login)
    $store = curl_exec($ch);

    //the login is now done and you can continue to get the
    //protected content.

    //set the URL to the protected file
    curl_setopt($ch, CURLOPT_URL, 'url to grab content from');

    //execute the request
    $content = curl_exec($ch);



    ?>
天空

我在您的代码中看到的问题是:

  • 您应该使用http_build_query编码数据。
  • 您应该启用CURLOPT_COOKIESESSION
  • 您应该CURLOPT_POST在登录后禁用
  • 最好使用用户代理,因为某些站点会阻止可疑的用户代理。
  • 同时使用CURLOPT_COOKIEFILECURLOPT_COOKIEJAR和使用'-'它存储在内存中。

这是最终代码:

<?php
$data = [
    'j_username' => $username,
    'j_password' => $password,
    'tk_trp' => $tk_trp
];
$ch = curl_init("http://example.com/login");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.8) Gecko/2009032609 Firefox/3.0.8');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_COOKIESESSION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, '-');
curl_setopt($ch, CURLOPT_COOKIEJAR, '-');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, "http://example.com/dashboard");
curl_setopt($ch, CURLOPT_POST, 0);
$x = curl_exec($ch);
echo $x;

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章