php 我们请求webapi的post请求啊?
如:
/api/WeiXin/PushMessageReceive
Post接收的 是一个对象
{
Id:''
"postSource": "string"
}
但是php的post过来的都是 name=val&name2=val2
这种格式. 怎么改进php的请求方式来适应 啊
没园豆 见谅
搜了好久还看见一个php或java调用webapi成功的例子..... 说好的跨平台呢. 别坑我啊.
去查下php那边用的httpclient包的用法,里面肯定会告诉你怎么把数据放到正文里
谢谢
<?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: content-type application/json
@吴瑞祥:
..... 可以了.. 晕
@吴瑞祥:
$header .= 'content-type: application/x-www-form-urlencoded;charset=UTF-8;'. "\r\n";
==>
$header .= 'content-type: application/json'. "\r\n";
后面不能带charset=UTF-8; 再想想什么原因
@colvinliu: 不是不能带那个。。content-type是描述你正文里的数据格式,
你正文里的数据格式是json,但你却说是form表单格式,服务端当然解析错误了