使用curl php提交表单

时间:2015-10-31 23:12:43

标签: php curl sms

我正在尝试使用PHP脚本通过网站发送短信,这是代码

function send_sms($phone,$message)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://www.fullonsms/home.php');
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Linux x86_64)       AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/32.0.1700.107     Chrome/32.0.1700.107 Safari/537.36');
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "MobileNos=".$phone."&message=".$message);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
    curl_setopt($ch, CURLOPT_COOKIESESSION, true);
    curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');  
    $text=curl_exec($ch);
    echo curl_getinfo($ch, CURLINFO_REDIRECT_URL);
}
send_sms("1234567890","hi");

我已在此网站www.fullonsms.com上创建了一个帐户,现在我正在尝试使用我的帐户发送短信。表单元素的ID是MobileNos和消息。你能告诉我为什么这不起作用。我错过了什么或这是完全错误的方法吗?

1 个答案:

答案 0 :(得分:1)

我不确定其余部分,您需要仔细查看请求的处理方式以及发送请求的页面,因为您要将内容发布到home.php

因此,在此之前,您需要使用curl登录系统。我正在为您提供示例功能,它将登录系统并设置cookie

function login($username,$password)
{
    $username = urlencode($username);
    $password = urlencode($password);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://www.fullonsms.com');
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Linux x86_64)       AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/32.0.1700.107     Chrome/32.0.1700.107 Safari/537.36');
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "MobileNoLogin=$username&LoginPassword=$password&captcha_val=10");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
    curl_setopt($ch, CURLOPT_COOKIESESSION, true);
    curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');   // cookie now set with logged in stuff. 
    $text=curl_exec($ch);
    curl_getinfo($ch, CURLINFO_REDIRECT_URL);
    //return $text; return and echo text if you want to view logged in page.
}

send_sms(...)之前调用此函数。

login(...);

同样在您的代码中,您不会将内容发布到右页。

curl_setopt($ch, CURLOPT_URL, 'http://www.fullonsms/home.php');

登录后应该是明显的

curl_setopt($ch, CURLOPT_URL, 'http://www.fullonsms.com/home.php');