Codeigniter - 通过URL发送数据并获得响应

时间:2014-11-28 08:42:26

标签: php codeigniter

我要做的是将一些数据发送到网址并获得“成功”之类的响应,表明数据是在codeigniter中接收的。我是否需要在codeigniter中配置api来测试它。我尝试了以下代码但没有得到预期的结果。提到我在codeigniter和php中都很新。

controller xmlPost.php

<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');


Class XmlPost extends CI_controller
{

public function index()
{

$this->load->model('xml_model','message');

$this->message->source = "14045" ;
$this->message->dst = "08887654657";
$this->message->msg = "Hi, this is test";
$this->message->url = 'http://localhost/codeigniter1/index.php/receive';

$data = $this->message->Send();

echo $data;
    }
}
?>

,模型是xml_model.php

<?php

Class Xml_model extends CI_Model {

 var $source; //Source address
 var $dst; //Destination
 var $msg; // Message
 var $url;
 var $api_url;


  function __construct() {
    parent::__construct();
   }

  function Send(){

    $this->api_url = 'http://localhost/codeigniter1/index.php/receive';
    $params = $this->source.'&destinationaddr='.$this->dst.'&shortmessage='.urlencode($this->msg).' '.urlencode($this->url).'';

   $post_data = array(
     "sourceaddr" => $params,
    );

    $stream_options = array(
    'http' => array(
       'method'  => 'POST',
    ),
   );

  $context  = stream_context_create($stream_options);
  $response = file_get_contents($this->api_url, null, $context);
  return $response;
        }
      }

      ?>

1 个答案:

答案 0 :(得分:1)

After url encoing Space[ ] can encoded as + sign.you have to manually replace as %20,then after execute your URL.
public function sendSMS($mobile,$code)
{

      $stream_options = array(
    'http' => array(
       'method'  => 'GET',
        ),
    );
             $context  = stream_context_create($stream_options);
     $url="http://api.smsbrain.in/1.2/appsms/send.php?user=satish123&passwd=123456&senderId=SATISH&recipients=".$mobile."&message=Your%20Verification%20code%20for%20facebook%20is%20".$code.".";
     //echo "URL".$url;
     $response = file_get_contents($url, null, $context);


    echo print_r($response);

}