如何使Curl后使用涉及Cookie的Php [英] How to make Curl post Using Php involving cookies

查看:90
本文介绍了如何使Curl后使用涉及Cookie的Php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个涉及Php Curl的脚本,使用 http://www.gysms.com/发送短信freesms.php
页面存储了一个cookie PHPSESSID,并且在发布过程中传递了一个名为token的隐藏字段。

I am developing a script involving Php Curl to send sms using http://www.gysms.com/freesms.php The page stores a cookie PHPSESSID and also a hidden field named token is passed during the posting.

我写了一个脚本涉及两个curl请求。第一个curl请求解析页面并获取令牌值。

I have written a script involving two curl requests. 1st curl request parse the page and obtain the token value .

以下是代码:

<?php

$phone = '9197xxxxxxx';
$msg = 'Hi this is curlpost';
$get_cookie_page = 'http://www.gysms.com/freesms.php';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $get_cookie_page);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$sabin = curl_exec($ch);
$html=explode('<input  type="hidden" name="trigger" value="',$sabin);
$html=explode('"/>',$html[1]);
//store the token value to $html[0]
?>

使用以下代码完成Curl发布:

Curl post is done using the following code:

<?php
$fields = array(
                            'trigger'=>urlencode($html[0]), //token value
                            'number'=>urlencode($phone),  //phone no
                            'message'=>urlencode($msg) //message

                );

//posting curl request
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
$url = 'http://www.gysms.com/freesms.php';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');

//execute post
$result = curl_exec($ch);
echo $result;
curl_close($ch);
?>

短信未发送使用上述代码。
如果发送短信应该显示短信被发送到 - 否。
我不知道我在哪里错了。请帮助,我是新的PHP。
最后这个尝试只针对我的教育用品。

The sms is not sending Using the above code. If the sms is sent It should show sms is send to-No. I don't Know where I went wrong. Please help, I am new to PHP. Finally this attempt is only for my educational purpouse.

推荐答案

这里是一些代码我想出的工作。希望它有帮助。关于您的代码的一些解释和反馈如下。

Here is some code I came up with that worked. Hope it helps. Some explanations and feedback about your code follow.

<?php

$number     = '14155556666';
$message    = 'This is my text in all its glory.';

$url        = 'http://www.gysms.com/freesms.php';
$cookieFile = tempnam(null, 'SMS');
$userAgent  = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:11.0) Gecko/20100101 Firefox/11.0';

if (strlen($message) > 100) {
    die('Message length cannot exceed 100 characters.');
}

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile);
curl_setopt($ch, CURLOPT_COOKIEJAR,  $cookieFile);
curl_setopt($ch, CURLOPT_USERAGENT,  $userAgent); // empty user agents probably not accepted
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER,    1); // enable this - they check referer on POST

$html = curl_exec($ch);

// <input  type="hidden" name="trigger" value="CXXrtmqVC7KbUnJ22UBodFy1kBj4ign5PsQ3qNR91nH2055307b4xP4"/>
if (!preg_match('/name=.trigger.\s+value=.([^\'"]+)/i', $html, $trigger)) {
    die('Failed to locate hidden input value');
}

sleep(5);  // without a slight delay, i often would not receive sms

$trigger = $trigger[1];

// build array of post values - all are important
$post = array('number'  => $number,
              'trigger' => $trigger,
              'message' => $message,
              'remLen'  => 100 - strlen($message),
              $trigger  => 'Send Message');

// switch request to POST, use http_build_query to encode post data for us
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));

$html = curl_exec($ch);

if (strpos($html, '<b>Message sent to</b>') !== false) {
    echo "Message sent!";
} else {
    echo "<b>Message not sent :(</b><br /><br />";
    echo $html;
}

我认为你可能有几个原因的麻烦:

I think you may have had trouble for several reasons:


  • 应在请求中指定User-Agent,如果您留空则拒绝。

  • http_build_query 构建POST字符串(首选项)

  • 您缺少2个字段在请求中, remLen 和触发器值作为提交按钮

  • 我经常不会收到消息,如果我没有

  • A User-Agent should be specified in the request, they seem to reject if you leave it empty
  • I used http_build_query to build the POST string (preference)
  • You were missing 2 fields in the request, remLen, and the trigger value as the submit button
  • I often would not receive the messages if I didn't sleep a few seconds before sending the message after getting the trigger value.

在大多数情况下,我没有收到消息,它仍然在屏幕上显示消息发送到手机#,即使它从来没有。一旦我结合了所有正确的事情(睡眠时间,用户代理,有效的职位字段),我会看到成功的消息,但也得到的响应。

In most of the cases where I didn't get the message, it still showed the "Message sent to phone #" on the screen even though it never came. Once I combined all the right things (sleep time, user agent, valid post fields) I would see the success message but also get the response.

我认为最关键你的代码遗漏的是,在第一个请求,你抓住触发器值,他们还设置了一个cookie( PHPSESSID ),你需要捕获。如果不在POST请求中发送,则可能是自动拒绝。

I think the most critical thing left out from your code was that on the first request where you grab the trigger value, they also set a cookie (PHPSESSID) that you are required to capture. Without sending that on the POST request it was probably an automatic reject.

要解决此问题,请确保您在第一个请求以及后续请求中捕获Cookie。我选择对这两个请求重复使用相同的curl句柄。您不是这样做,必须在请求之间使用相同的cookie文件和cookie罐。

To get around this, make sure you capture cookies on the first request as well as subsequent requests. I chose to re-use the same curl handle for both requests. You don't have to do it that way, but you would have to use the same cookie file and cookie jar between requests.

希望有帮助。

这篇关于如何使Curl后使用涉及Cookie的Php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆