前兩天,第三方合作公司給我們一個Webservice的鏈接,說是Java做的,里面有個sendMsg()方法,讓我們在用php做的項目里推送消息給他們。我們公司是有用.net做的Webservice,而Java的Webservice沒用過。
他們提供的東西:
1. Java Webservice:http://不給看IP:9080/mccweb/webservice/common/wsMessageService?wsdl
2. Java Webservice里的方法:sendMsg(string username, string title, string content, string code)
php調用代碼:
1 include('./NuSOAP/lib/nusoap.php'); 2
3 $client = new nusoap_client('http://不給看IP:9080/mccweb/webservice/common/wsMessageService?wsdl','wsdl'); 4 $err = $client->getError(); 5 if ($err) { 6 echo '<h2>Constructor error</h2><pre>' . $err . '</pre>'; 7 } 8
9 $param['username'] = 'andychen'; 10 $param['title'] = 'test push'; 11 $param['content'] = 'hello world!'; 12 $param['code'] = '@w#wed%d#dkesq#@!!324skfjsds!'; 13
14 $result = $client->call('sendMsg', array('parameters' => $param)); 15 echo $result;
運行結果:
我一看,尼瑪“http://mccwebhost”這什么鬼??
瀏覽器打開“http://不給看IP:9080/mccweb/webservice/common/wsMessageService?wsdl”看看;
原來是這里出來的,那,這又是什么鬼??
我一想php是通過soap去調用Webservice的,且報錯信息里有后面這個值,哦,了解了。“http://mccwebhost”這個是無效域名,被寫死了。
溝通半天對方說改不了這是生成的,啊??生成的?鬼信。最后對方說他們用Nginx做了負載均衡,要了賬號上服務器看了一下,我看到nginx.conf是這樣的:
呵呵,好了是做了Nginx轉發,做的不徹底,是這樣的 Nginx轉發
改成這樣:
1 upstream mccwebhost{ 2 ip_hash; 3 server 127.0.0.1:8082; 4 } 5
6 location /mccweb/ { 7 proxy_redirect off; 8 proxy_set_header Host $host:9080; 9 proxy_set_header X-Real-IP $remote_addr; 10 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 11 proxy_pass http://mccwebhost;
12 }
重啟Nginx,再次瀏覽器打開“http://不給看IP:9080/mccweb/webservice/common/wsMessageService?wsdl”看看;
好了,可以了。再運行php代碼,返回0。對方說是參數傳遞不成功。為什么????經過搜索得知,要這樣:
1 include('./NuSOAP/lib/nusoap.php'); 2
3 $client = new nusoap_client('http://不給看IP:9080/mccweb/webservice/common/wsMessageService?wsdl','wsdl'); 4 $err = $client->getError(); 5 if ($err) { 6 echo '<h2>Constructor error</h2><pre>' . $err . '</pre>'; 7 } 8
9 $param['arg0'] = 'andychen'; 10 $param['arg1'] = 'test push'; 11 $param['arg2'] = 'hello world!'; 12 $param['arg3'] = '@w#wed%d#dkesq#@!!324skfjsds!'; 13
14 $result = $client->call('sendMsg', array('parameters' => $param)); 15 echo $result;
再次運行php代碼,返回1。成功!為什么要這樣傳參呢,依據如圖: