2013-06-29 5 views
0

현재 curl을 통해 클라이언트에 데이터를 반환하는 PHP 파일이 있습니다. 클라이언트를 JSON에 응답하도록 변경하여 문제를 처리하고 모든 작업을 훌륭하게 처리합니다. 하지만 이제 JSON 객체로 데이터를 반환 할 수 있습니다. 그것은 현재 내가 관심이없는 HTML 태그 등을 많이 반환합니다. 예를 들어 원 텍스트를이 형식의 텍스트로 반환하고 싶습니다. C5 : 3;php curl to json response

다음은 올바른 방향으로 나를 가리킬 수있는 코드입니다.

//set POST variables 
    $url = $_POST['url']; 
    unset($_POST['url']); 
    $fields_string = ""; 
    //url-ify the data for the POST 
    foreach($_POST as $key=>$value) { 
    $fields_string .= $key.'='.urlencode($value).'&'; 
    } 
    rtrim($fields_string,"&"); 
    //open connection 
    $ch = curl_init(); 
    //set the url, number of POST vars, POST data 
    $url .= '&'; 
    $url .= $fields_string; 
    curl_setopt($ch,CURLOPT_URL,$url); 
    //execute post 

    $string = curl_exec($ch); 

    curl_close($ch); 

나는이 같은 JSON 개체를 반환 찾고 있어요

$data = array('success'=> true,'message'=>'C5;3'); 

이 클라이언트 코드입니다.

 $("button.checkStatus").click(function(){ 
      //This Ajax checks the current on/off status of the passed X10 code    
      $('img.checkStatus').each(function(i, obj) { 
       var element = $(this); 
       $x10Device = $(this).data("x10"); 
       var postData = "url=http://192.168.0.34:81/tenHsServer/tenHsServer.aspx?t=ab&f=DeviceStatus&d=" + $x10Device ; 



$.ajax({ 
    type: "POST", 
    dataType: "json", 
    data: postData, 
    beforeSend: function(x) { 
     if(x && x.overrideMimeType) { 
      x.overrideMimeType("application/json;charset=UTF-8"); 
     } 
    }, 
    url: 'urlencodeJson.php', 
    success: function(data) { 
     // 'data' is a JSON object which we can access directly. 
     if (data.success == true){ 
      $('#section1').html(data.message); 
      //$("x10").data('src','lightbulbon.png'); 
      element.attr('src','lightbulbon.png'); 
    } 
     else{ 
      $('#section2').html(data.message);  
      element.attr('src','lightbulbon.png'); 
     } 
    } 
    }); 
+0

답장은 무엇입니까? –

+1

대상 페이지'$ url'에서 코드를 보겠습니다. 내가 너를 올바르게 이해한다면, 그게 너를 변화시킬 필요가있는 곳이야. – TecBrat

답변

0
<?php 
header("Content-Type: text/plain"); //if html elements exists in array, it will echo as plain text 

$yourarray = array('success'=> true,'message'=>'C5;3'); 

echo json_encode($yourarray); 
?> 
+0

LOL, 나는 그 질문을 제안했지만 그의 질문은 분명하지 않았다. 그는 html 태그를 여러 개 가져 왔지만 배열을 보여줍니다. –

+0

내가 클라이언트 측에서하고있는 일. –

0

의 단지가 유 컬 결과를받은 후 결과를 PHP에서 HTML을 구문 분석하고 얻을 수있는 매우 강력한 PHP 클래스 u는 그

$url = $_POST['url']; 
    unset($_POST['url']); 
    $fields_string = ""; 
    //url-ify the data for the POST 
    foreach($_POST as $key=>$value) { 
    $fields_string .= $key.'='.urlencode($value).'&'; 
    } 
    rtrim($fields_string,"&"); 
    //open connection 
    $ch = curl_init(); 
    //set the url, number of POST vars, POST data 
    $url .= '&'; 
    $url .= $fields_string; 
    curl_setopt($ch,CURLOPT_URL,$url); 
    //execute post 

    $string = curl_exec($ch); 

    curl_close($ch); 
    require 'simple_html_dom.php'; 
    $html= new simple_html_dom(); 
    $html->load($string); 
    foreach($html->find('div#selector') as $article) { 
$article['title']=$article->find('div#title',0)->plaintext; 
$article['photo']=$article->find('img#id',0)->atrr('id'); 
$article['content']=$article->find('div#content',0)->plaintext; 
} 
처럼 어떤 일을 할 수 simple_html_dom를 사용 할 매우 간단합니다

유 PHP 배열(); json array()로 변환 할 수 있습니다. json_encode() 예를 들어 방문하십시오. curl result to JSON