2013-05-13 82 views
0

尋找一些Ruby腳本後,我試圖用PHP寫一些幫助腳本。 我現在的問題是,我不確定jSon對象是否正確,因此我現在不知道它的來源。蘋果保修檢查腳本

我的問題是,如果我在PHP中犯了jSon錯誤?如果不是源的對象是錯誤的。

<?php 

$sn = isset($_GET['sn']) ? $_GET['sn'] : ''; 

if($sn) 
{ 
    $url = 'https://selfsolve.apple.com/warrantyChecker.do?sn='.$sn . "&country=USA"; 

    $json = file_get_contents($url); 
    $json = substr($json, 5, -1); 
    $json_obj = json_decode($json); 

    if(isset($json_obj->ERROR_CODE)) 
    { 
     echo $json_obj->ERROR_DESC; 
    } 
    else 
    { 
     echo "$json_obj->PROD_DESCR <img src=\"$json_obj->PROD_IMAGE_URL\" alt=\"\"><br>"; 

     echo"Product Description: $json_obj->PROD_DESCR <br>"; 
     echo"Purchase date: $json_obj->PURCHASE_DATE <br>"; 
     echo"Warranty exp date: $json_obj->COVERAGE_DATE <br>"; 

    } 
} 
?> 

<form action="" method="get" accept-charset="utf-8"> 
    <p><input name="sn" value="<?=$sn?>"><input type="submit" value="Lookup serial"></p> 
</form> 

另一種方式我試圖做到這一點是

<?php 
$sn = $argv[1]; 
$data = json_decode(file_get_contents( 
     "https://selfsolve.apple.com/warrantyChecker.do?sn=". $sn . "&country=USA")); 
echo "Product Description" .$data->PROD_DESCR."\n"; 
echo "Coverage for " . $sn . " ends on " . $data->COVERAGE_DATE . "\n"; 
?> 
+0

「我不知道現在它的來源。」你什麼意思? – meda 2013-05-13 02:50:24

+0

嘗試通過curl從目標url獲取響應..並且在json解碼之前也不要使用substring .. – 2013-05-13 07:40:29

回答

0
<?php 

$sn = isset($_GET['sn']) ? $_GET['sn'] : ''; 

if($sn) 
{ 
    $url = 'https://selfsolve.apple.com/warrantyChecker.do?sn='.$sn . "&country=USA"; 

    $json = file_get_contents($url); 

    //This line you are splitting the json form then it wont work 
    $json = substr($json, 5, -1); 

    $json_obj = json_decode($json); 

    if(isset($json_obj->ERROR_CODE)) 
    { 
     echo $json_obj->ERROR_DESC; 
    } 
    else 
    { 
     echo "$json_obj->PROD_DESCR <img src=\"$json_obj->PROD_IMAGE_URL\" alt=\"\"><br>"; 

     echo"Product Description: $json_obj->PROD_DESCR <br>"; 

     echo"Purchase date: $json_obj->PURCHASE_DATE <br>"; 

     echo"Warranty exp date: $json_obj->COVERAGE_DATE <br>"; 


    } 
} 
?>