CURL登录&提交另一篇文章

时间:2012-11-16 12:16:34

标签: php post curl login

为了学习PHP,我的老板让我做了一些项目。到目前为止,我已经做了一个待办事项列表&提醒(www.frontpagewebdesign.com/newfolder)但我现在正在尝试做的是发送短信通知。

因为我无法为这么小的项目购买短信网关,所以我决定在这个网站上使用我的账户:www.sms-gratuite.ro。我的麻烦是使用CURL自动登录和短信发送。

我遵循了一个教程,这是我到目前为止所做的:

<?php

$form_vars = array();
//array for SMS sending form values

$username = '****@****.com';
$password = '********';
$loginUrl = 'http://sms-gratuite.ro/page/autentificare';
$postUrl='http://sms-gratuite.ro/page/acasa';
$form_vars['to'] = "076xxxxxxx"; 
//my own phone number
$form_vars['mesaj'] = "test";
//SMS text
$encoded_form_vars = http_build_query($form_vars);
$user_agent="Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";

//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 (mail and parola are the IDs of the form input fields)
curl_setopt($ch, CURLOPT_POSTFIELDS, 'mail='.$username.'&parola='.$password);

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

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);

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

//check if the Login was succcesful by finding a string on the resulting page
if(strpos($store, "Trimite mesaj")===FALSE)
echo "logged in";
else
echo "not logged";

//set the landing url
curl_setopt($ch, CURLOPT_URL, 'http://sms-gratuite.ro/page/autentificare');

$referer='';

curl_setopt($ch, CURLOPT_URL, $postUrl);
//curl_setopt($ch, CURLOPT_HTTPHEADER,array("Expect:"));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_REFERER, $referer);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'to='.$form_vars['to'].'&mesaj='.$form_vars['mesaj']);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);

$result = curl_exec($ch);

if(strpos($result, "Mesajul a fost trimis")===FALSE)
echo "<br>sms sent";
else
echo "<br>sms not sent";

curl_close($ch);

?>

我没有任何错误,但肯定不起作用。首先登录失败。这个形式是一个特殊的形式,卷曲无法处理吗?

1 个答案:

答案 0 :(得分:2)

  1. 您可以将每个curl请求结果放入不同的文件中,例如page1.html,page2.html等。这样您就可以在浏览器中打开,看看您获得的回报的确切页面是什么
  2. 您需要像浏览器一样提出完全相同的请求。有像HttpFox这样的浏览器插件(如果你使用的是FireFox)可以显示所有已发送的字段,所有cookie以及其他所有相关内容。您可以将这些列表与您的卷曲形成的内容进行比较,以找到缺少的部分
  3. 尝试theese的步骤,并对你得到的更多错误发表评论,最好是详细的。

相关问题