/** Generic request using the protocol: * POSTs data & files, checks the magic header * @param array $post Arbitrary data to POST * @param array $files Files to upload: { name: path | [path, filename] | [path, filename, mimetype] } * @return HttpResponse * @throws RemScriptProtocolError */ function _request($post, $files) { # Prepare $request = new HttpRequest($this->script_url, 'POST'); $request->mimicBrowser(); $request->headers['Connection'] = 'Close'; $request->post($post); if (!empty($files)) { foreach ($files as $name => $upload) { list($path, $filename, $mimetype) = (array) $upload + array(null, null, null); $request->upload($name, $path, $filename, $mimetype); } } # Request try { $response = $request->open(); } catch (HttpRequestError $e) { throw new RemScriptProtocolError('Request error: ' . $e->getMessage(), RemScriptProtocolError::REQUEST_ERROR, $e); } # Response: check code if ($response->code != 200) { throw new RemScriptProtocolError('Response code: ' . $response->code); } # Response: check magic $expected = self::RESPONSE_MAGIC; $actual = fread($response->f, strlen($expected)); if ($actual !== $expected) { throw new RemScriptProtocolError('Wrong magic: ' . var_export($actual, 1)); } # All okay return $response; }
/** * 2012年6月28日 携程 唐春龙 研发中心 * 通过httpRequest调用远程webservice服务(返回一个XML) * @param $responseUrl 远程服务的地址 * @param $requestXML 远程服务的参数请求体XML * @param 返回XML */ function httpRequestSoapData($responseUrl, $requestXML) { try { $myhttp = new HttpRequest($responseUrl . "?WSDL", "POST"); //--相对于API2.0固定 $r_head = <<<BEGIN <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <Request xmlns="http://ctrip.com/"> <requestXML> BEGIN; //--相对于API2.0固定 $r_end = <<<BEGIN </requestXML> </Request> </soap:Body> </soap:Envelope> BEGIN; //返回头--相对于API2.0固定 $responseHead = <<<begin <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><RequestResponse xmlns="http://ctrip.com/"><RequestResult> begin; //返回尾--相对于API2.0固定 $responseEnd = <<<begin </RequestResult></RequestResponse></soap:Body></soap:Envelope> begin; $requestXML = str_replace("<", @"<", $requestXML); $requestXML = str_replace(">", @">", $requestXML); $requestXML = $r_head . $requestXML . $r_end; //echo "<!--" . $requestXML ."-->"; $myhttp->open(); $myhttp->send($requestXML); $responseBodys = $myhttp->getResponseBody(); //这里有可能有HEAD,要判断一下 if (strpos($responseBodys, "Content-Type: text/xml; charset=utf-8")) { $coutw = $myhttp->responseBodyWithoutHeader; } else { $coutw = $responseBodys; } //$myhttp->responseBodyWithoutHeader; //$coutw=$myhttp->responseBodyWithoutHeader; $coutw = str_replace($responseHead, "", $coutw); //替换返回头 $coutw = str_replace($responseEnd, "", $coutw); //替换返回尾 $coutw = str_replace("<", "<", $coutw); //将符号换回来 $coutw = str_replace(">", ">", $coutw); //将符号换回来 // echo $coutw; return $coutw; } catch (SoapFault $fault) { return $fault->faultcode; } }
if ($data->firstChild && $data->firstChild->nodeType == XML_TEXT_NODE) { return iconv('UTF-8', 'ISO-8859-2', $data->firstChild->nodeValue); } $ret = array(); foreach ($data->childNodes as $var) { $value = $var->nodeType == XML_ELEMENT_NODE && !$var->hasChildNodes() ? '' : parse_data($var); if ($var->nodeType == XML_ELEMENT_NODE && $var->hasAttribute('name')) { $ret[$var->getAttribute('name')] = $value; } else { $ret[] = $value; } } return $ret; } $hr = new HttpRequest(); $hr->open($_SERVER['REQUEST_METHOD'], 'http://www.forumweb.pl' . substr($_SERVER['REQUEST_URI'], strlen(substr($_SERVER['SCRIPT_NAME'], -1) == '/' ? $_SERVER['SCRIPT_NAME'] : dirname($_SERVER['SCRIPT_NAME']))) . (strpos($_SERVER['REQUEST_URI'], '?') === false ? '?' : '&') . '_format=xml'); foreach (apache_request_headers() as $name => $value) { if (!in_array(strtolower($name), array('host', 'accept-encoding'))) { $hr->setRequestHeader($name, $value); } } $hr->send(file_get_contents('php://input')); header('HTTP/1.1 ' . $hr->status . ' ' . $hr->statusText); foreach (explode("\r\n", $hr->getAllResponseHeaders()) as $value) { if (preg_match('/^Set-Cookie:/i', $value)) { header(preg_replace(array('/(;\\s+path=)[^;]+/', '/(;\\s+domain=)[^;]+/'), array('$1' . rtrim(dirname($_SERVER['SCRIPT_NAME']), '/') . '/', '$1' . $_SERVER['SERVER_NAME']), $value)); } else { if (!preg_match('/^Content-Type:/i', $value)) { header($value); } }