Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/246.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php (二);投稿_Php_Post_Curl_Login - Fatal编程技术网

Php (二);投稿

Php (二);投稿,php,post,curl,login,Php,Post,Curl,Login,为了学习PHP,我的老板让我做一些项目。到目前为止,我已经完成了一个待办事项列表和提醒(www.frontpagewebdesign.com/newfolder),但我现在要做的是发送短信通知 因为我买不起这么小的项目的短信网关,所以我决定使用我在这个网站上的账户:www.SMS-returne.ro。我的问题是自动登录和短信发送与卷曲 我遵循了一个教程,到目前为止我做了以下工作: <?php $form_vars = array(); //array for SMS sending f

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

因为我买不起这么小的项目的短信网关,所以我决定使用我在这个网站上的账户:www.SMS-returne.ro。我的问题是自动登录和短信发送与卷曲

我遵循了一个教程,到目前为止我做了以下工作:

<?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);

?>

我没有任何错误,但它肯定不起作用。首先,登录失败。这是一种特殊的形状,旋度不能处理它吗

  • 您可以将每个curl请求结果放入不同的文件中,例如page1.html、page2.html等。这样,您就可以在浏览器中打开并查看您的请求得到的确切页面
  • 您需要发出与浏览器完全相同的请求。有一些浏览器插件,比如HttpFox(如果您使用的是FireFox),可以向您显示发送的所有字段、所有cookie以及其他相关内容。您可以将该列表与卷曲形成的列表进行比较,以找到缺少的片段

  • 请尝试这些步骤,并对您发现的更多错误进行评论,最好是详细的评论。

    首先,您应该与网站的内容取得联系,并在那里寻求帮助。。。或者获得提供
    REST
    API的其他服务。。。但对于你的问题,它应该有效。在提交登录请求之前,请确保您拥有网站设置的所有cookie。。。此外,他们可能会检查用户代理变量,您可以尝试仿冒它,例如
    Mozilla/4.0(兼容;MSIE 5.01;Windows NT 5.0)
    谢谢Kamil!做了编辑,我想我成功登录了。至少浏览器是这么说的。使用相同的方法(strpos),我尝试提交第二个表单,但没有得到积极的结果。我无法访问该表单来实际提交SMS/一般来说:确保表单中的所有HTML
    输入都在
    CURLOPT_POSTFIELDS
    中,包括
    type=“submit”
    如果它具有
    名称
    属性。此外,您还应将第一次调用(登录)中的
    CURLOPT_COOKIEJAR
    作为
    CURLOPT_COOKIEFILE
    传递到第二次调用,以将登录调用中获取的cookie传递到sms调用,以伪造正常访问。此类信息不容易获取。我在脚本中添加了您的建议,并成功登录。很高兴听到您的建议得到了解决…:-)它没有被广泛发布的原因是,服务提供商不喜欢他们的服务以这种方式使用:-)我通过了第一点(登录),现在我被卡在了第二点。我从Firebug获得了一些关于HTTP请求细节的信息,我试图理解它以及curl选项,并认为我涵盖了所有HTTP头。我不知道有这样的插件。我到家后会试试的,我会通知你的。谢谢