我正在尝试进行登录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);
?>
答案 0 :(得分:1)
我在您的代码中看到的问题是:
http_build_query
对数据进行编码。CURLOPT_COOKIESESSION
。CURLOPT_POST
。CURLOPT_COOKIEFILE
和CURLOPT_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;
答案 1 :(得分:0)
可能是&tk_trp和=中有多余的空间吗? curl_setopt($ ch,CURLOPT_POSTFIELDS,'j_username ='。$ username。'&j_password ='。$ password。'&tk_trp = '。$ tk_trp);