doyp9057 2017-06-24 07:52
浏览 228
已采纳

如何将PHP类转换为json数据?

hello guys i want make json data and post to my URL this is my new code but not work because i don't know how to fix it I actually want to turn this whole information into json and send it to my requested link. Thank you for your help

    class Info
{
    public $name; //String
    public $_postman_id; //String
    public $description; //String
    public $schema; //String
}

class Header
{
    public $key; //String
    public $value; //String
    public $description; //String
}

class Formdata
{
    public $key; //String
    public $value; //String
    public $type; //String
    public $disabled; //bool?
}
class Body

{
    public $mode; //String
    public $formdata; //array(Formdata)
}

class Request
{
    public $url; //String
    public $method; //String
    public $header; //array(Header)
    public $body; //Body
    public $description; //String
}

class Item
{
    public $name; //String
    public $request; //Request
    public $response; //array(Object)
}

class xibo
{
    public $variables; //array(Object)
    public $info; //Info
    public $item; //array(Item)
}

$json_data = json_encode((array) xibo);
print_r($json_data);

$URL = "HTTP://87.98.148.67/";    
$content = json_encode("mahdi");

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER,
       array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);

$json_response = curl_exec($curl);

$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

if ( $status != 201 ) {
    die("ersal nashod" . curl_error($curl) . ", curl_errno " . curl_errno($curl));
}


curl_close($curl);

$response = json_decode($json_response, true);
echo $content;
  • 写回答

1条回答 默认 最新

  • douzhanlie9209 2017-06-24 12:33
    关注

    @Sushiant, your question got many downvotes, because your code has many trivial mistakes. Don't worry about it. Everybody was nubies. Keep calm and learn.

    My feedback is:

    1. Use $varName = new ClassName() for creating an object. The object is an instance of the class.
    2. Use json_encode for encode a variable to a json string.
    3. Use __construct in your classes
    4. Read any base PHP book or course.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型