[英]change php curl to json response
currently I have a php file that returns the data to the client via curl, I have changed the client to respond to JSON, to handle a problem and all works great. 目前,我有一个php文件,它通过curl将数据返回给客户端,我已经更改了客户端以响应JSON,以处理问题,并且一切正常。 But now is it possible to return the data as JSON object.
但是现在可以将数据作为JSON对象返回。 It currently Returns alot of html tags etc that I am not interested in,I would like to return just the raw text as an example the text is in this format C5:3;
它当前返回很多我不感兴趣的html标记等,我只想返回原始文本作为示例,该文本的格式为C5:3;
Below is the code would appreciate if anyone could point me in the right direction. 如果有人能指出正确的方向,下面的代码将不胜感激。
//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);
I am looking to return a JSON object like this, 我希望返回这样的JSON对象,
$data = array('success'=> true,'message'=>'C5;3');
This is the client code. 这是客户端代码。
$("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');
}
}
});
<?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);
?>
its very simple to do it just use simple_html_dom it a very powerful PHP class that u can parse html from PHP and get result after getting curl result u can do some thing like that 它非常简单,只需使用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;
}
u have a php array(); 你有一个php array(); u can convert to json array() php
json_encode()
to see more example visit convert curl result to JSON 您可以将其转换为json array()php
json_encode()
以查看更多示例,将curl结果转换为JSON
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.