将php curl更改为json响应

时间:2013-06-29 18:42:23

标签: php json

目前我有一个php文件,通过curl将数据返回给客户端,我已经更改了客户端以响应JSON,处理问题并且一切正常。但现在可以将数据作为JSON对象返回。它目前返回很多我不感兴趣的html标签等,我想只返回原始文本作为示例,文本格式为C5:3;

如果有人能指出我正确的方向,下面代码会很感激。

//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');
        }
    }
   });

2 个答案:

答案 0 :(得分: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);
?>

答案 1 :(得分:0)

它非常简单,只需使用simple_html_dom它就是一个非常强大的PHP类,你可以从PHP解析html并在获得curl结果后得到结果你可以做那样的事情

$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;
}
你有一个php数组(); 你可以转换为json array()php json_encode() 要查看更多示例,请访问convert curl result to JSON