doyp9057
2017-06-24 07:52
浏览 197

如何将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;

图片转代码服务由CSDN问答提供 功能建议

你好我想要制作json数据并发布到我的网址 这是我的新代码但是没有用,因为我不知道 我不知道如何解决它 我实际上想把这整个信息转换成json并将其发送到我要求的链接。 谢谢你的帮助

  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;  //请求
 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)); 
} 
 
 
 ncurl_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.
    打赏 评论

相关推荐 更多相似问题