/** * Change XML to an array * * @param string $data * @return array */ public static function XML2array($data) { $result = array(); if (is_object($data) || is_array($data)) { if (is_object($data)) { $temp = get_object_vars($data); } else { $temp = $data; } if (!$temp) { return ''; } foreach ($temp as $key => $value) { $result[$key] = XMLUtil::XML2array($value); } } else { return $data; } return $result; }
/** * request * 01 查询单据轨迹 02 保存定单 03回写单据号 04 更改单据状态 * @param string $data * @return string 0 记录不存在 1 成功 -1 失败 -2 用户/密码错误 -3 无效机构号 -9 其他错误 */ public function request($data) { try { //$this->addLog("kenlong"); if (C("DEBUG_MODE")) { $this->addLog("query::data:" . $data); } $xml = simplexml_load_string($data); $request = XMLUtil::XML2array($xml); //检查用户/密码是否正确 if (!$this->check($request["head"], $result)) { return $result; } //开始请求处理 switch ($request["head"]["type"]) { case '01': $result = $this->track($request); break; case '02': $result = $this->order($request); break; case '0200': $result = $this->orderQuery($request); break; case '03': $result = $this->updatebillno($request); break; case '04': $result = $this->updatestatus($request); break; default: $result = $this->createResult('-9'); } } catch (Exception $e) { $this->addLog("Not exception error from query:" . $e->getMessage()); $result = $this->createResult($request["head"]["type"]); } return $result; }
/** * control * * @param string $data * @param string $add * @return string */ public function query($data) { if (C("DEBUG_MODE")) { $this->addLog("query::data:" . $data); } try { $xml = simplexml_load_string($data); $request = XMLUtil::XML2array($xml); //数据格式不正确 if (!$request) { return $this->createResult($request["head"]["type"], '-3', 'what you request?'); } //用户密码不正确 if ($request["head"]["user"] != "user4soap" || $request["head"]["pass"] != 'pass4soap') { return $this->createResult($request["head"]["type"], '-2'); } switch (substr($request["head"]["type"], 0, 2)) { case '01': $rs = $this->billsQuery($request); break; case '02': $rs = $this->order($request); break; case '03': $rs = $this->uploadawb($request); break; default: $rs = $this->createResult($request["head"]["type"], '0', "not a valid action"); } return $rs; } catch (Exception $e) { if (C("DEBUG_MODE")) { $this->addLog("query error:" . print_r($e, true)); } return 'false'; } }
/** * 同步员工 * * @param string $data * @return boolean */ private function synMember($data) { //id或表明为空,则返回失败 if (!$data["ObjectId"] || !$data["ObjectName"]) { return false; } //删除 if ($data["MethodName"] == '3') { $result = $this->memberDelete($data["ObjectId"]); if ($result) { return true; } else { return false; } } //获取信息 $params = array("userid" => $data["ObjectId"]); $result = $this->client->call("GetUsersInfoById", array('parameters' => $params), '', ''); $result = $result["GetUsersInfoByIdResult"]; if ($result == '-2') { $this->addLog("access denied in synMember!"); return false; } //将GBK替换成UTF-8,否则simplexml_load_string会失败 $result = str_replace("GBK", "utf-8", $result); $xml = simplexml_load_string($result); $rs = XMLUtil::XML2array($xml); if (!$rs) { return false; } $info = $this->getLogInfo($rs["Node"]["property"]); //返回值无id,则返回失败 if (!$info["DeptId"]) { return false; } //添加 if ($data["MethodName"] == '1') { $result = $this->memberAdd($info); } else { $result = $this->memberUpdate($info); } //返回值 if ($result) { return true; } else { return false; } }
/** * 下载订单 * */ public function ReadFromSPL() { $this->create(); try { //call //$result = unserialize(base64_decode(file_get_contents("d:/result.log"))); $result = $this->service->call("setTmsShipment", array('tmsShipment' => $this->authname), '', ''); if (DEBUG) { $this->debug("resultSerialize", base64_encode(serialize($result))); } if (!$result) { $this->addLog("the webservice was no result."); $this->addLog($this->service->error_str); return false; } if (!$result["item"]) { $this->addLog("the item as empty."); return false; } if (DEBUG) { $this->debug("readdata", print_r($result["item"], true)); } $readItem = array(); if (is_array($result["item"])) { $readItem = $result["item"]; } else { $readItem[] = $result["item"]; } foreach ($readItem as $data) { $xml = simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA); $response = XMLUtil::XML2array($xml); $messagedetail = simplexml_load_string(trim($response['MESSAGEDETAIL'])); $contentlist = array(); $contentlist[] = $messagedetail->CONTENTLIST; if (DEBUG) { $this->debug("contentlist", print_r($contentlist, true)); } foreach ($contentlist as $tmp) { if (DEBUG) { $this->debug("temp", print_r($tmp, true)); } $tmp = XMLUtil::XML2array($tmp); $tmp = $tmp['CONTENT']; $order = $tmp['HEADER']; $details = array(); if (array_key_exists(0, $tmp['DETAIL'])) { $details = $tmp['DETAIL']; } else { $details[] = $tmp['DETAIL']; } $succeed = 0; if (DEBUG) { $this->debug("details", print_r($details, true)); } foreach ($details as $detail) { if (DEBUG) { $this->debug("detail", print_r($detail, true)); } $order = array_merge($order, $detail); $order['FILEFUNCTION'] = $response['MESSAGEHEAD']['FILEFUNCTION']; if (!$this->saveOrder($order)) { $this->addLog("saveOrder data error in function ReadFromSPL"); $succeed += 1; } } // 如果都成功了,反馈信息 if ($succeed === 0) { //回写已下载标记 $content = "<CONTENTLIST>" . "<CONTENT>" . "<HEADER></HEADER>" . "<DETAIL>" . "<ECNO>" . $this->orderno . "</ECNO>" . "<CECNO></CECNO>" . "<FLAG>1</FLAG>" . "</DETAIL>" . "</CONTENT>" . "</CONTENTLIST>"; $senddata = trim($this->createResult('DispatcFlag', $content)); if (DEBUG) { $this->debug("response", htmlspecialchars_decode($senddata)); } if (!$this->orderno) { $this->addLog("ecno in setTmsShipmentType is empty, not good"); continue; } //call if (in_array($this->orderno, $this->sendBack)) { $this->addLog("bill was sendback"); } else { $rs = $this->service->call("setTmsShipmentType", array('tmsShipmentTypeXml' => $senddata), '', ''); if (DEBUG) { $this->debug("responseresult", print_r($rs, true)); } $this->checkResult($rs); // 休息一下 $this->sleep4awhile(); } } } } } catch (Exception $e) { $this->addLog("setTmsShipment Exception error" . $e->getMessage()); return false; } }