如何使用curl将数据发布到此网站?

时间:2014-06-27 14:23:08

标签: php function curl

我正在开发一个使用clicksend SMS的HTTP API发送短信的项目。我正在尝试使用curl函数发布数据,但我遇到了困难。

这些是帖子字段:

  

的方法,用户名,密钥,到,消息发送者

我希望使用我的HTML表单使用curl函数来完成它。

    <?php
if(isset($_POST['number'])) {
 $pass = $_POST['password'];
 $num = $_POST['number'];
 $msg = $_POST['msg'];
 $sender = $_POST['sender'];

  // Passwords Start Here.....!!
 if ($pass == "jony") {
 echo "Correct Password....!!"; 
}
 else {
 echo "Wrong Password....!! ";
exit;
}
// Passwords End Here....!!
echo "Success   "  . $num . "  ";


 $LOGINURL = "https://api.clicksend.com/http/v2/send.php?method=http&username=username&key=KEY";
$POSTFIELDS = '&to=' . $num .'&message=' . $msg . '&senderid=' . $sender;
$agent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0";
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,$LOGINURL);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS,$POSTFIELDS); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch,CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_REFERER, $reffer); 
   curl_exec ($ch);
?>

2 个答案:

答案 0 :(得分:3)

我假设你已经删除了登录网址中的实际用户名和密钥,对吧?如果没有,您将需要使用ClickSend提供的那些。毫无疑问,您将能够在帐户中设置API密钥等...

此外,我发现CURLOPT_RETURNTRANSFER设置为1,但由于您现在正在调试,因此应将其设置为0(这将显示来自运行脚本后单击“发送”

其余的看起来很好。一旦你将ClickSend响应呈现到页面,它就可以让你更好地了解问题的根源。

尝试运行此脚本,告诉我们页面的内容。

<?php
  //You *MUST* define your username and key. It is availible in your ClickSend dashboard.
  $_my_clicksend_username = "clicksendusername";
  $_my_clicksend_key = "abc123";
  //You *MUST* define the 'to', 'message' and 'senderid'
  $to        = "the recipient";
  $message   = "the message";
  $senderid  = "the sender ID";
  //Tell CURL where to post, and what data to send.
  $url = "https://api.clicksend.com/http/v2/send.php?method=http&username=$_my_clicksend_username&key=$_my_clicksend_key";
  $data = '&to=' . $to .'&message=' . $message . '&senderid=' . $senderid;
  //Now tell CURL to POST the data and write the outcome on the page. 
  $ch = curl_init(); 
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_POST, 1); 
  curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0); 
  curl_exec ($ch);
  curl_close($ch);
?>

答案 1 :(得分:-1)

你几乎是对的。

唯一的事情是你只需要遵循某些事情,就是这样,不需要在那里结账用户代理。而且您似乎正在集成SMS API。

所以格式就是这样的。

// Send the POST request with cURL
$data = "user=username&&pass=pasword";
$ch = curl_init('http://yourdomainname.com/filename.php');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch); //This is the result from Textlocal
curl_close($ch);

我相信这可能已经足够了。