首页 新闻 会员 周边 捐助

php 我们请求webapi的post请求啊?

0
悬赏园豆:5 [已解决问题] 解决于 2015-09-01 16:06

php 我们请求webapi的post请求啊?

 

如:

/api/WeiXin/PushMessageReceive

Post接收的 是一个对象
{
Id:''
  "postSource": "string"
}


但是php的post过来的都是 name=val&name2=val2

这种格式. 怎么改进php的请求方式来适应 啊



没园豆 见谅
问题补充:

搜了好久还看见一个php或java调用webapi成功的例子..... 说好的跨平台呢. 别坑我啊.

colvinliu的主页 colvinliu | 初学一级 | 园豆:6
提问于:2015-09-01 09:43
< >
分享
最佳答案
0

去查下php那边用的httpclient包的用法,里面肯定会告诉你怎么把数据放到正文里

收获园豆:5
吴瑞祥 | 高人七级 |园豆:29449 | 2015-09-01 09:47

谢谢

colvinliu | 园豆:6 (初学一级) | 2015-09-01 09:49
<?php


$path= '/WeiXin/PushMessageReceive'; 
$body = "{'dataId':'57452bac-58bc-4a3b-80bc-985ee22eb1a8','code':'0000','msg':'发送成功'}";
 
define('API_URL', 'http://xxxxxxx/api/');
define('APP_KEY', '85F91326'); // 8位
define('API_TOKEN', '7F8D1FF912956C69B7DEC3E1FA43F3'); //32位
define('API_RESTYPE', 'json');




httpClient::init($httpClient, array( 'debugging' => true , 'userAgent' => 'MSIE 15.0' ));
$httpClient->post(API_URL.$path, $body);

echo $httpClient->request; // 获取 请求头部信息
echo $httpClient->response; // 获取 响应的头部信息
echo $httpClient->buffer; // 获取 网页内容

















class httpClient {

 public $buffer = null;  // buffer 获取返回的字符串
 public $referer = null;  // referer 设置 HTTP_REFERER 的网址
 public $response = null; // response 服务器响应的 header 信息
 public $request = null;  // request 发送到服务器的 header 信息
 private $args = null;

 public static function init(&$instanceof, $args = array()) {
  return $instanceof = new self($args);
 }

 private function __construct($args = array()) {

  if(!is_array($args)) $args = array();
  $this->args = $args;
  if(!empty($this->args['debugging'])) {
   ob_end_clean();
   set_time_limit(0);
   header('Content-Type: text/plain; charset=utf-8');
  }

 }

 public function get($url, $data = null, $cookie = null) {

  $parse = parse_url($url);
  $url .= isset($parse['query']) ? '&'. $data : ( $data ? '?'. $data : '' );
  $host = $parse['host'];

  $header  = 'Host: '. $host. "\r\n";
  $header .= 'Connection: close'. "\r\n";
  $header .= 'Accept: */*'. "\r\n";
  $header .= 'User-Agent: '. ( isset($this->args['userAgent']) ? $this->args['userAgent'] : $_SERVER['HTTP_USER_AGENT'] ). "\r\n";
  $header .= 'DNT: 1'. "\r\n";
  if($cookie) $header .= 'Cookie: '. $cookie. "\r\n";
  if($this->referer) $header .= 'Referer: '. $this->referer. "\r\n";

  $options = array();
  $options['http']['method'] = 'GET';
  $options['http']['header'] = $header;

  $response = get_headers($url);
  $this->request = $header;
  $this->response = implode("\r\n", $response);
  $context = stream_context_create($options);
  return $this->buffer = file_get_contents($url, false, $context);

 }

 public function post($url, $data = null, $cookie = null) {

  $parse = parse_url($url);
  $host = $parse['host'];
  $header ='';

  $ch = curl_init();
  $time = time();
  $sign = sprintf('app_key=%s&app_secret=%s&time=%s', APP_KEY, API_TOKEN, $time);
  $sign = strtoupper(md5($sign));
           
  $header .= 'content-type: application/x-www-form-urlencoded;charset=UTF-8;'. "\r\n";
  $header .= 'Accept: application/json'. "\r\n";
  $header .= 'app_key:' . APP_KEY. "\r\n";
  $header .= 'sign:' . $sign. "\r\n";
  $header .= 'time:' . $time. "\r\n";


  
  $header .= 'DNT: 1'. "\r\n";
  if($cookie) $header .= 'Cookie: '. $cookie. "\r\n";
  if($this->referer) $header .= 'Referer: '. $this->referer. "\r\n";
  if($data) $header .= 'Content-Length: '. strlen($data). "\r\n";

  $options = array();
  $options['http']['method'] = 'POST';
  $options['http']['header'] = $header;
  if($data) $options['http']['content'] = $data;

  $response = get_headers($url);
  $this->request = $header;
  $this->response = implode("\r\n", $response);
  $context = stream_context_create($options);
  return $this->buffer = file_get_contents($url, false, $context);

 }

}
?>

帮我看看吧. 实在是不知道怎么整.

 

现在HttpActionContext.Request.Content能接受到数据. 但是不能正常转化为接口的 参数实例.(实例为空)

 

colvinliu | 园豆:6 (初学一级) | 2015-09-01 13:44

@colvinliu: content-type application/json

吴瑞祥 | 园豆:29449 (高人七级) | 2015-09-01 14:07

@吴瑞祥: 

 

..... 可以了.. 晕

colvinliu | 园豆:6 (初学一级) | 2015-09-01 14:10

@吴瑞祥: 

 

 

$header .= 'content-type: application/x-www-form-urlencoded;charset=UTF-8;'. "\r\n";

 

==>

 

  $header .= 'content-type: application/json'. "\r\n";

 

后面不能带charset=UTF-8;  再想想什么原因

 

 

colvinliu | 园豆:6 (初学一级) | 2015-09-01 14:12

@colvinliu: 不是不能带那个。。content-type是描述你正文里的数据格式,

你正文里的数据格式是json,但你却说是form表单格式,服务端当然解析错误了

吴瑞祥 | 园豆:29449 (高人七级) | 2015-09-01 15:11
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册