PHP如何通过Post请求发送Json数据 如何向php服务器发送数据为json的post请求
php\u4e2d\u901a\u8fc7post\u8bf7\u6c42\u5e76\u63a5\u6536json\u6570\u636e$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $uri );
curl_setopt ( $ch, CURLOPT_POST, 1 );//\u8bbe\u7f6e\u4e3apost
curl_setopt ( $ch, CURLOPT_HEADER, 0 );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );//data\u4e3apost\u8bf7\u6c42\u7684\u53c2\u6570
$return = curl_exec ( $ch );//$return\u4e3a\u63a5\u6536\u5230\u7684\u8fd4\u56de\u503c
curl_close ( $ch );
\u7528curl
$ch = curl_init();
$header[] = "Content-type: text/xml";//\u5b9a\u4e49content-type\u4e3axml
curl_setopt($ch, CURLOPT_URL, $url); //\u5b9a\u4e49\u8868\u5355\u63d0\u4ea4\u5730\u5740
curl_setopt($ch, CURLOPT_POST, 1); //\u5b9a\u4e49\u63d0\u4ea4\u7c7b\u578b 1\uff1aPOST \uff1b0\uff1aGET
curl_setopt($ch, CURLOPT_HEADER, 1); //\u5b9a\u4e49\u662f\u5426\u663e\u793a\u72b6\u6001\u5934 1\uff1a\u663e\u793a \uff1b 0\uff1a\u4e0d\u663e\u793a
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//\u5b9a\u4e49\u8bf7\u6c42\u7c7b\u578b
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);//\u5b9a\u4e49\u662f\u5426\u76f4\u63a5\u8f93\u51fa\u8fd4\u56de\u6d41
curl_setopt($ch, CURLOPT_POSTFIELDS, $postStr); //\u5b9a\u4e49\u63d0\u4ea4\u7684\u6570\u636e\uff0c\u8fd9\u91cc\u662fXML\u6587\u4ef6
$res = curl_exec($ch);
curl_close($ch);//\u5173\u95ed
首先要把数据转换成json格式,再通过curl方法调用接口并传参数
代码如下:
$keyword = urlencode($_POST['keyword']);
$parameters = json_encode(array('keyWord'=>$keyword,'areaCode'=>'*'));
$post_data['appToken'] = "323ds7674354fds32fdsda60173";//随便写的
$post_data['parameters'] = $parameters;
$url = 'http://serde.com/compadddvd/index';//随便写的
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);//用post方法传送参数
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
之后就返回数据即可。
前端通过ajax发起post请求 后端查询到结果后使用json_decode()把数据转成json格式后 return返回 前端就可以通过回调函数拿到json数据
绛旓細index.html椤甸潰 //杩欐槸post璇锋眰鏂规硶 //杩欐槸get璇锋眰鏂规硶 data.php椤甸潰 <?php//鎵撳嵃鍏ㄥ眬鏁扮粍print_r($_POST);//浣滅敤鏄墦鍗板嚭浣犳彁浜ょ殑鏁版嵁銆俻rint_r($_GET);?>浠g爜鍙互鐩存帴鎷垮埌鐜涓祴璇曪紝绁濅綘鏃╂棩鎴愬姛銆
绛旓細
绛旓細鍙互鐢 Curl銆乻ocket銆乫ile_get_contents 绛夋柟寮忔潵鍙戦丳OST鏁版嵁
绛旓細curl 灏濊瘯澶氳璇绘墜鍐岀殑浠g爜锛屽綋涓殑涓浜涢夐」鑷繁鐮旂┒鐮旂┒鍐欏啓娴嬫祴锛岃繖涓唬鐮佺洰鍓嶈繕鐪熸病鏈夌湅杩囩綉涓婃湁浠涔堝ソ鐨勭幇鎴愮殑銆
绛旓細甯綘璋冭瘯浜嗕竴涓嬩綘鐨勪唬鐮侊紝鍙憄ost璇锋眰锛岋紝鏁版嵁鐨勬牸寮忔槸锛'name=zhangsan&age=18' 锛岃繖绉嶅瓧绗︿覆鏍煎紡鐨勶紝鎵浠ヤ綘瑕佹妸浣犵殑 var senddata="4"; 鏀规垚 var senddata="senddata=4";php 閮ㄥ垎锛実etDeviceID=$_POST[senddata];鏀规垚$getDeviceID=$_POST['senddata'];鎺ユ敹鐨勫弬鏁伴渶瑕佸姞涓婂崟寮曞彿銆傚彟澶栧鏋滄槸...
绛旓細curl_setopt($ch, CURLOPT_URL, $url); //瀹氫箟琛ㄥ崟鎻愪氦鍦板潃 curl_setopt($ch, CURLOPT_POST, 1); //瀹氫箟鎻愪氦绫诲瀷 1锛歅OST 锛0锛欸ET curl_setopt($ch, CURLOPT_HEADER, 1); //瀹氫箟鏄惁鏄剧ず鐘舵佸ご 1锛氭樉绀 锛 0锛氫笉鏄剧ず curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//瀹氫箟璇...
绛旓細 curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); if ($json) { //鍙戦JSON鏁版嵁 ...
绛旓細//鎵撳嵃鑾峰緱鐨勬暟鎹畃rint_r($output);POST鏂规硶:url = "http://localhost/web_services.php";$post_data = array ("username" => "bob","key" => "12345");$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);// post鏁版嵁curl_...
绛旓細琛ㄥ崟鐩存帴浼犻掞紝浠g爜濡備笅锛佸垱寤篻o.php 鏂囦欢涓殑浠g爜濡備笅锛<?php@$name = $_POST['name'];if(!empty($name)){ echo $name;}else{ echo ' 鎻愪氦';}?> 浠g爜瑙i噴 name = $_POST['name'];閿欒鎶戝埗 1銆佸父瑙佸彉閲$name = post杩囨潵鐨刵ame鍊 2銆乪mpty($name)妫鏌ユ槸鍚︿负绌猴紝鍦...
绛旓細浣犺鐨勬槸 php curl鍚 url = 'http鍦板潃';//鎺ユ敹XML鍦板潃 $header = "Content-type: text/xml";//瀹氫箟content-type涓簒ml $ch = curl_init(); //鍒濆鍖朿url curl_setopt($ch, CURLOPT_URL, $url);//璁剧疆閾炬帴 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//璁剧疆鏄惁杩斿洖淇℃伅...