duanmianxue2687 2013-04-04 16:37
浏览 67
已采纳

使用cURL将POST数据发送到API [关闭]

I can send sms using the following form

<form action="http://myserviceprovider/myname/server.php" method="post"> 
 <input  type="hidden" value="MyUsername" name="user" />   
 <input  type="hidden" value="MyPassword" name="pass" />           
 <input  type="hidden" value="MyKey" name="sid" />    
 <input  type="hidden" value="12345678" name="sms[0][0]" /> 
 <input  type="hidden" value="MyFrist SMS Text" name="sms[0][1]" />
 <input  type="hidden" value="97654321" name="sms[1][0]" />                    
 <input  type="hidden" value="MySecond SMS Text" name="sms[1][1]" />
 <input type="submit" />
</form> 

Now I am trying to send sms using PHP cURL. I have created an array which contains cellphone numbers and messages:

$sms =array(
           array("0" => "12345678", "1" => "MyFrist SMS Text"),
           array("0" => "97654321", "1" => "MySecond SMS Text")
      );

The problem is I cannot figure out how exactly to send the values including username,password and Mykey using the following

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($post));
curl_exec($ch);

Could you please tell how to solve this problem?

  • 写回答

2条回答 默认 最新

  • dpy87530 2013-04-04 16:51
    关注

    As far as I undersand you would want to post to the FORM URL with cURL, instead of using the site. You need to send the other form fields as well as the sms information, i.e you need to pass to the form url your Username, Password, Sid.

    An example script could look like this:

    <?php
    define('SMS_SERVICE_URL', 'http://myserviceprovider/myname/server.php');
    define('USERNAME', 'YOUR_USERNAME_HERE');
    define('PASSWORD', 'YOUR_PASSWORD_HERE');
    define('KEY', 'YOUR_KEY/SID_HERE');
    
    $sms =array(
               array("0" => "12345678", "1" => "MyFrist SMS Text"),
               array("0" => "97654321", "1" => "MySecond SMS Text")
    );
    
    
    $post = array(
            'user' => USERNAME,
            'pass' => PASSWORD,
            'sid'  => KEY,
            'sms'  => $sms,
    );
    
    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL, SMS_SERVICE_URL);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
    
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    $result = curl_exec ($ch);
    
    curl_close ($ch);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)