~~简~~

..

PHP Json POST Server&Client

class IndexController extends Controller {

	/**
	 */
	public function index() {
		$body = $this->getRawBody();
		$jsonArray = json_decode($body,true);
		if(($jsonArray['name'] == 'bizcndomaintrade') && ($jsonArray['password'] == 'Aa654123')) {
			$dao = D('Users');
			$userInfo = $dao->where(array('user_name'=>$jsonArray['checkname'] ))->find();
			if(empty($userInfo))
				$this->ajaxReturn(array('status'=>530,'msg'=>'用户不存在' ),'JSON',JSON_UNESCAPED_UNICODE);
			$affectRowCount = $dao->where(array('user_id'=>$userInfo['user_id'],'partner_id'=>$userInfo['bindingid'] ))->delete();
			if(!$affectRowCount) {
				$this->ajaxReturn(array('status'=>200,'msg'=>'unbind success.' ));
			}
			$this->ajaxReturn(array('status'=>532,'msg'=>'bind failure.' ));
		}
		else {
			$this->ajaxReturn(array('status'=>531,'msg'=>'requester is not auth .' ));
		}
	}
	
	protected function getRawBody() {
		return $body = file_get_contents('php://input');
	}
	
	public function post() {
		$url = 'http://www.domaintrade.com/servicex/index/index/';
		$jsonArray = array(
			'name'=>'bizcndomaintrade',
			'password'=>'Aa654123',
			'checkname'=>'jie',
			'bindingid'=>'1'
		);
		$headers = array(
				"POST ".$url." HTTP/1.1",
				"Content-type: text/xml",
				"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
				"Cache-Control: no-cache",
				"Pragma: no-cache",
		);
		$timeout = 300;
		$post = json_encode($jsonArray);
		if (function_exists('curl_init')) {
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL,$url);
			curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
			curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
			curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
			curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false); // don't check certificate
			curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false); // don't check certificate
			curl_setopt($ch, CURLOPT_SSLVERSION, 4);
			curl_setopt($ch,CURLOPT_HEADER,false); // true Return the HTTP headers in string
			curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); // a true curl_exec return content
			if ($post) {
				curl_setopt($ch, CURLOPT_POST, 1);
				if (is_array($post)) {
					$post = http_build_query($post);
				}
				curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
			}
			$successs = 1;
			$contents = curl_exec($ch);
			if(!$contents) {
				$successs = 0;
			}
			curl_close($ch); // close curl resource
			echo $contents;
		}
	}
}


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhouzhiande/article/details/49945767
文章标签: PHP
个人分类: PHP
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

PHP Json POST Server&Client

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭