2013-01-07 2 views
0

XML 데이터를 게시하여 작동하는 간단한 API 인 php를 사용하여 사용자 정의 api를 작성했습니다. 내가 API에 게시하기 위해 노력하고있다 코드는 다음과 같습니다송신 응답 - PHP API

<?php 
$postdata = file_get_contents("php://input"); 
$xml = simplexml_load_string($postdata); 
$first = $xml->first; 
$last = $xml->last; 
$email = $xml->email; 
$phone = $xml->phone; 
?> 

가 그럼 난 그 PHP 변수를 취하고로 전송 님의 다른 측면에

<?php 
$xml_data = '<document> 
<first>'.$first.'</first> 
<last>'.$last.'</last> 
<email>'.$email.'</email> 
<phone>'.$phone.'</phone> 
<body>TEST</body> 
</document>'; 
     $URL = "url"; 
     $ch = curl_init($URL); 
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
     curl_setopt($ch, CURLOPT_POST, 1); 
     curl_setopt($ch, CURLOPT_HEADER, 0); 
     curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml')); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml_data"); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
     $output = curl_exec($ch); 
     curl_close($ch); 
     $Response = curl_exec($ch);  
    curl_close($ch); 
    echo "Responce= ".$responce; 
?> 

, 코드 위의 게시물 데이터베이스 ..이 코드는 모두 작동 중입니다 !!

하지만 내 질문은 : 게시 측에 응답을 어떻게 보냅니 까? curl_init을 사용하여 curl_exec으로 보내려면 어떻게해야합니까?

도움이 될 것입니다. 당신에게 제이슨

+1

에서 돌아 볼 수 다음을 수행 후 데이터를 처리하는 스크립트, XML 응답을 반환? – Madbreaks

+0

실수, 미안 해요! 출력과 닫기를 꺼내고 응답과 닫기를 남깁니다. – user1789437

답변

2

감사 내가 원하는 생각 :

echo "Responce= ".$Response; 
        ^^^ 
+0

또 다른 실수. :( – user1789437

1

당신이 다른 내용에 대해서와 동일 할 거라고 응답을 반환하려면 헤더를 설정하고 출력을 에코. 예를 들어,

<?php 
$postdata = file_get_contents("php://input"); 
$xml = simplexml_load_string($postdata); 
$first = $xml->first; 
$last = $xml->last; 
$email = $xml->email; 
$phone = $xml->phone; 

// do your db stuff 

// format response 
$response = '<response> 
    <success>Hello World</success> 
</response>'; 
// set header 
header('Content-type: text/xml'); 
// echo xml identifier and response back 
echo chr(60).chr(63).'xml version="1.0" encoding="utf-8" '.chr(63).chr(62); 
echo $response; 
exit; 
?> 

당신은 응답이 왜 두 번`curl_exec` 호출 curl_exec()

+0

그건 100 % 일 했어요! – user1789437