URL에서 XML 파일을 읽어야 합니다.
$map_url = "http://maps.google.com/maps/api/directions/xml?origin=".$merchant_address_url."&destination=".$customer_address_url."&sensor=false";
다음과 같은 URL이 표시됩니다:
http://maps.google.com/maps/api/directions/xml?origin=Quentin+로드+브룩클린%2C+뉴욕%2C+11234+유나이티드스테이트&목적지=550+매디슨애비뉴+뉴욕%2C+뉴욕%2C+10001+유나이티드스테이트&센서=false
이 함수를 사용하여 데이터를 읽은 다음 가져오고 있습니다:
$response_xml_data = file_get_contents($map_url);
if($response_xml_data){
echo "read";
}
$data = simplexml_load_string($response_xml_data);
echo "<pre>"; print_r($data); exit;
하지만 운이 없네요, 도움이 필요하세요?
함수를 사용하여 XML에서 데이터를 가져올 수 있습니다. 이 링크를 참조하세요.
http://php.net/manual/en/function.simplexml-load-file.php
$url = "http://maps.google.com/maps/api/directions/xml?origin=Quentin+Road+Brooklyn%2C+New+York%2C+11234+United+States&destination=550+Madison+Avenue+New+York%2C+New+York%2C+10001+United+States&sensor=false";
$xml = simplexml_load_file($url);
print_r($xml);
코드가 올바른 것 같습니다. fopen wrappers이 활성화되어 있는지 확인합니다(php.ini에서 allow_url_fopen = On
).
또한 다른 답변에서 언급했듯이 올바르게 인코딩된 URI를 제공하거나 urlencode() 함수를 사용하여 인코딩해야 합니다. 또한 XML 문자열을 가져 오는 데 오류가 있는지, 구문 분석 오류가 있는지 확인해야하며, 다음과 같이 libxml_get_errors()을 사용하여 출력 할 수 있습니다:
<?php
if (($response_xml_data = file_get_contents($map_url))===false){
echo "Error fetching XML\n";
} else {
libxml_use_internal_errors(true);
$data = simplexml_load_string($response_xml_data);
if (!$data) {
echo "Error loading XML\n";
foreach(libxml_get_errors() as $error) {
echo "\t", $error->message;
}
} else {
print_r($data);
}
}
?>
요청에 사용자 정의 헤더를 포함해야 하기 때문에 XML 코드를 가져올 수 없는 경우, 예제 4의 file_get_contents()
를 호출할 때 사용할 사용자 정의 스트림 컨텍스트를 생성하기 위해 stream_context_create()를 사용하는 방법을 http://php.net/manual/en/function.file-get-contents.php 에서 확인하세요.
'보통' () 는 file_get_contents 권한 문제. 이를 방지하기 위해 사용합니다.
function get_xml_from_url($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
$xmlstr = curl_exec($ch);
curl_close($ch);
return $xmlstr;
}
예:
$xmlstr = get_xml_from_url('http://www.camara.gov.br/SitCamaraWS/Deputados.asmx/ObterDeputados');
$xmlobj = new SimpleXMLElement($xmlstr);
$xmlobj = (array)$xmlobj;//optional
url http://www.example.com & # 39, & # 39 '$ =;; = $ ($ uirl " LIBXML_NOCDATA SimpleXMLElement",,), xml simpleXML_load_file '
$ uirl php 파일을 생성할 수 있는 만큼 데이터가 xml 파일 형식으로 출력입니다.