doulieyu0881
2012-01-08 16:59 阅读 12
已采纳

Php Curl发布到.aspx

I am failing to post using curl in php to the url: http://example.com/sms/sms.aspx

The post string should be in the pattern:example.com/sms/sms.aspx?uid=9746674889&pwd=passwod&phone=9746674889&msg=hjgyjgKJKJK&provider=way2sms&send=SEND

If it is posted the output will Be 1 else -1

Can anyone help me in the curl part

<?php


//extract data from the post
  // extract($_GET);

           $uid = $_GET['uid'];
           $phone = $_GET['phone'];
           $pwd = $_GET['pwd'];
           $msg = $_GET['msg'];
           $provider = 'way2sms';


//set POST variables



$fields = array(
                            'uid'=>rawurlencode($uid),
                            'phone'=>rawurlencode($phone),
                            'pwd'=>rawurlencode($pwd),
                            'msg'=>rawurlencode($msg),
                            'provider'=>rawurlencode($provider)
                );

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');

///curl part(please help)

?>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    doukanwa6872 doukanwa6872 2012-01-08 17:05

    Here is a basic cURL POST script. It has no error handling - you should definitely read the cURL manual.

    <?php
    
      // Destination URL
      $url = 'http://ubaid.tk/sms/sms.aspx';
    
      // Raw data to send
      $fields = array(
       'uid' => $_GET['uid'],
       'phone'=>$_GET['phone'],
       'pwd'=>$_GET['pwd'],
       'msg'=>$_GET['msg'],
       'provider'=>'way2sms'
      );
    
      // Build $fields into an encoded string
      $body = http_build_query($fields);
    
      // Initialise cURL
      $ch = curl_init($url);
    
      // Set options (post request, return body from exec)
      curl_setopt($ch, CURLOPT_POST, TRUE);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    
      // Do the request
      $result = curl_exec($ch);
    
      // Echo the result
      echo $result;
    

    EDIT

    Since what you actually want is GET and not POST, the code gets a lot simpler:

    <?php
    
      // Destination URL
      $url = 'http://ubaid.tk/sms/sms.aspx';
    
      // Raw data to send
      $fields = array(
       'uid' => $_GET['uid'],
       'phone'=>$_GET['phone'],
       'pwd'=>$_GET['pwd'],
       'msg'=>$_GET['msg'],
       'provider'=>'way2sms'
      );
    
      // Build $fields into an encoded string and append to URL
      $url .= '?'.http_build_query($fields);
    
      // Initialise cURL
      $ch = curl_init($url);
    
      // Set options (post request, return body from exec)
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    
      // Do the request
      $result = curl_exec($ch);
    
      // Echo the result
      echo $result;
    

    ...or this whole thing could be done without cURL as:

    <?php
    
      // Destination URL
      $url = 'http://ubaid.tk/sms/sms.aspx';
    
      // Raw data to send
      $fields = array(
       'uid' => $_GET['uid'],
       'phone'=>$_GET['phone'],
       'pwd'=>$_GET['pwd'],
       'msg'=>$_GET['msg'],
       'provider'=>'way2sms'
      );
    
      // Build $fields into an encoded string and append to URL
      $url .= '?'.http_build_query($fields);
    
      // Do the request
      $result = file_get_contents($url);
    
      // Echo the result
      echo $result;
    
    点赞 评论 复制链接分享

相关推荐