2016-08-19 3 views
0

私は現在、file_get_contentsを使用して取得したJSONファイルのnew_userという特定の値を検索してから問題を解読しています。ここで特定の値を見つけるためにPHPでJSONを解析できませんか?

は私のコードです:

<html> 
<head> 
    <link href="styles.css" rel="stylesheet" type="text/css" /> 
<title>PHP Bing</title> 
</head> 
<body><form method="post" action="<?php echo $PHP_SELF;?>"> 
Type in a search:<input type="text" id="searchText" name="searchText" value=""/> 
     <input type="submit" value="Search!" name="submit" id="searchButton" /> 
<?php 
if (isset($_POST['submit'])) { 
$request = 'https://devblogs.instavoice.com/vb?data={"cmd":"join_user","client_os_ver":"1511","client_app_ver":"vb.00.00.001","phone_num":"xxxxxxxxxxxxx","device_id":"xxxxxxxxxxxxx","phone_num_edited":"false","opr_info_edited":"false","sim_opr_mcc_mnc":"na","sim_country_iso":"in","api_ver":"2","app_secure_key":"xxxxxxxxxxx","client_os":"w","sim_serial_num":"xxxxxxxxxxxxxx"}'; 
$response = file_get_contents($request); 
$jsonobj = json_decode($response); 
foreach($json as $item) 
{ 
    if($item->new_user == "false") 
    { 
     echo "ssdasdSA"; 
    } 
else { 
echo "Sdasds"; 
} 
} 
} ?> 
</form> 
</body> 
</html> 

そして、ここでは私がnew_userを検索しようとしていますJSON出力です:

{ 
    "cmd": "join_user", 
    "status": "ok", 
    "login_id": "918220524411", 
    "user_secure_key": "b2ff39178db49257733ff76c093329c484589459ccc617167a2a98d40dc5359a3ff89b54ab04889c", 
    "invite_sms_text": "Hi, I\u0027m using InstaVoice for Voicemail, Missed Calls, and Chatting. Connect with me on", 
    "vsms_limits": "{\"limit\":50,\"perusr_limit\":50,\"debit\":1,\"credit\":1,\"users\":[]}", 
    "docs_url": "http://devblogs.instavoice.com/vobolo/iv/docs/", 
    "mqtt_hostname": "devmqtt.instavoice.com", 
    "mqtt_port_ssl": 8883, 
    "mqtt_password": "guest", 
    "mqtt_user": "guest", 
    "iv_support_contact_ids": "[{\"support_catg_id\":\"IVSupport\",\"support_catg\":\"Help\",\"show_as_iv_user\":true,\"iv_user_id\":\"2624836\",\"phone\":\"912222222222\",\"profile_pic_uri\":\"http:\\/\\/devblogs.instavoice.com\\/vobolo\\/profile-images\\/2222\\/2624836_help_a.png\",\"thumbnail_profile_pic_uri\":\"http:\\/\\/devblogs.instavoice.com\\/vobolo\\/thumbnails\\/2222\\/2624836_help_a.png\",\"support_send_iv\":true,\"support_send_sms\":false,\"support_send_email\":false},{\"feedback_catg_id\":\"IVFeedback\",\"feedback_catg\":\"Suggestions\",\"show_as_iv_user\":false,\"iv_user_id\":\"2624835\",\"phone\":\"911111111111\",\"profile_pic_uri\":\"http:\\/\\/devblogs.instavoice.com\\/vobolo\\/profile-images\\/1111\\/2624835_suggestions.png\",\"thumbnail_profile_pic_uri\":\"http:\\/\\/devblogs.instavoice.com\\/vobolo\\/thumbnails\\/1111\\/2624835_suggestions.png\",\"feedback_send_iv\":true,\"feedback_send_sms\":false,\"feedback_send_email\":false}]", 
    "iv_user_id": 17198589, 
    "iv_user_device_id": 61712, 
    "screen_name": "68318465", 
    "is_profile_pic_set": false, 
    "profile_pic_uri": "http://devblogs.instavoice.com/vobolo/static-contents/images/default_profile_pic.jpg", 
    "thumbnail_profile_pic_uri": "http://devblogs.instavoice.com/vobolo/static-contents/images/default_profile_pic_thumbnail.jpg", 
    "facebook_connection": false, 
    "twitter_connection": false, 
    "fb_connected": false, 
    "tw_connected": false, 
    "fb_post_enabled": true, 
    "tw_post_enabled": true, 
    "fb_connect_url": "http://devblogs.instavoice.com/iv/fbc/", 
    "tw_connect_url": "http://devblogs.instavoice.com/iv/twc/", 
    "vsms_allowed": true, 
    "country_isd": "91", 
    "phone_len": 10, 
    "last_fetched_msg_id": 0, 
    "last_fetched_contact_trno": 0, 
    "last_fetched_msg_activity_id": 0, 
    "last_fetched_profile_trno": 0, 
    "send_email_for_iv": true, 
    "send_sms_for_iv": true, 
    "send_email_for_vb": true, 
    "send_sms_for_vb": true, 
    "send_email_for_vsms": true, 
    "send_sms_for_vsms": true, 
    "action": "", 
    "new_user": true, 
    "phone_num_edited": false, 
    "user_verified": true, 
    "obd_timegap_sec": 45, 
    "ring_expiry_min": 30, 
    "chat_hostname": "devmqtt.instavoice.com", 
    "chat_port_ssl": "8883", 
    "chat_user": "guest", 
    "chat_password": "guest" 
} 
+0

あなたは何の変数 '$のjson'を持っていない、それは' $のjsonobj'です。 – Barmar

答えて

1

は「偽」の文字列を比較していませんが、ブール値にfalse

$jsonobj = json_decode($response); 
if($jsonobj->new_user === true) echo 'new user'; 
else echo 'not new user'; 

Live demo

+0

ありがとう!私が$ user_secure = $ jsonobj-> user_secure_keyを使用すると、なぜエラーが発生するのですか?エラーは通知です:未定義のプロパティ:stdClass :: $ user_secure_key –

+0

'$ jsonobj-> user_secure_key'は問題なく動作します(https://3v4l.org/Gcc6mを参照)。 '$ jsonobj - > $ user_secure_key'のように、プロパティ名の前に' $ 'を使うとエラーが出ます。なぜなら' $ user_secure_key'はプロパティの名前ではないからです(未定義の変数です) – BeetleJuice

0

JSONは配列ではないため、foreachループは必要ありません。また、new_userは文字列ではなくブール値なので、文字列と比較しないでください。

if ($jsonobj->new_user) { 
    echo "Sdasds"; 
} else { 
    echo "ssdasdSA"; 
} 
0

私が得たすべての元データは、データ内に問題があることを示唆する構文エラーです。

$j='{"cmd":"join_user","status":"ok","login_id":"918220524411","user_secure_key":"b2ff39178db49257733ff76c093329c484589459ccc617167a2a98d40dc5359a3ff89b54ab04889c","invite_sms_text":"Hi, I\u0027m using InstaVoice for Voicemail, Missed Calls, and Chatting. Connect with me on","vsms_limits":"{\"limit\":50,\"perusr_limit\":50,\"debit\":1,\"credit\":1,\"users\":[]}","docs_url":"http://devblogs.instavoice.com/vobolo/iv/docs/","mqtt_hostname":"devmqtt.instavoice.com","mqtt_port_ssl":8883,"mqtt_password":"guest","mqtt_user":"guest","iv_support_contact_ids":"[{\"support_catg_id\":\"IVSupport\",\"support_catg\":\"Help\",\"show_as_iv_user\":true,\"iv_user_id\":\"2624836\",\"phone\":\"912222222222\",\"profile_pic_uri\":\"http:\\/\\/devblogs.instavoice.com\\/vobolo\\/profile-images\\/2222\\/2624836_help_a.png\",\"thumbnail_profile_pic_uri\":\"http:\\/\\/devblogs.instavoice.com\\/vobolo\\/thumbnails\\/2222\\/2624836_help_a.png\",\"support_send_iv\":true,\"support_send_sms\":false,\"support_send_email\":false},{\"feedback_catg_id\":\"IVFeedback\",\"feedback_catg\":\"Suggestions\",\"show_as_iv_user\":false,\"iv_user_id\":\"2624835\",\"phone\":\"911111111111\",\"profile_pic_uri\":\"http:\\/\\/devblogs.instavoice.com\\/vobolo\\/profile-images\\/1111\\/2624835_suggestions.png\",\"thumbnail_profile_pic_uri\":\"http:\\/\\/devblogs.instavoice.com\\/vobolo\\/thumbnails\\/1111\\/2624835_suggestions.png\",\"feedback_send_iv\":true,\"feedback_send_sms\":false,\"feedback_send_email\":false}]","iv_user_id":17198589,"iv_user_device_id":61712,"screen_name":"68318465","is_profile_pic_set":false,"profile_pic_uri":"http://devblogs.instavoice.com/vobolo/static-contents/images/default_profile_pic.jpg","thumbnail_profile_pic_uri":"http://devblogs.instavoice.com/vobolo/static-contents/images/default_profile_pic_thumbnail.jpg","facebook_connection":false,"twitter_connection":false,"fb_connected":false,"tw_connected":false,"fb_post_enabled":true,"tw_post_enabled":true,"fb_connect_url":"http://devblogs.instavoice.com/iv/fbc/","tw_connect_url":"http://devblogs.instavoice.com/iv/twc/","vsms_allowed":true,"country_isd":"91","phone_len":10,"last_fetched_msg_id":0,"last_fetched_contact_trno":0,"last_fetched_msg_activity_id":0,"last_fetched_profile_trno":0,"send_email_for_iv":true,"send_sms_for_iv":true,"send_email_for_vb":true,"send_sms_for_vb":true,"send_email_for_vsms":true,"send_sms_for_vsms":true,"action":"",**"new_user":true**,"phone_num_edited":false,"user_verified":true,"obd_timegap_sec":45,"ring_expiry_min":30,"chat_hostname":"devmqtt.instavoice.com","chat_port_ssl":"8883","chat_user":"guest","chat_password":"guest"}'; 

$json=json_decode($j,true); 
if(json_last_error()!==JSON_ERROR_NONE){ 

    $errs=array(
     JSON_ERROR_DEPTH => 'Maximum stack depth exceeded', 
     JSON_ERROR_CTRL_CHAR=> 'Unexpected control character found', 
     JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON', 
     JSON_ERROR_NONE  => 'No errors' 
    ); 
    echo $errs[ json_last_error() ]; 
} else { 
    foreach($json as $key => $value){ 
     /* do stuff */ 
    } 
} 
0

コードに単純なバグがあるようです。 反復しようとしていたもの($json)は未定義です。

また$PHP_SELFが未定義しているように見える foreach($json as $item)

foreach($jsonobj as $item)に変更してみてください。私は、あなたが探しているかもしれないと思う:

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">

関連する問題