0

所以我试图让这个工作无济于事。下面是我的 PHP 文件和我的 JSON 数据。

请注意,我必须先将 JSONP 转换为 JSON。那工作得很好。现在看着这个疯狂的数据数组,我不知道如何在 foreach 循环中获取变量。任何帮助都会是很好的帮助。

谢谢

我的文件.php

$url = "http://www.SwingAcademy.com/web/external/api.ashx?method=getLessons&     callback=?";  
$groupID = "43AB3367-F58D-487E-B2C6-2571A3A3508C";
$num_results = "";

$post_data = array ( 
    "NUMRESULTS" => $num_results,
    "GROUPID" => $groupID,
    "OWNERID" => ''

);  


$curl_options = array(
    CURLOPT_URL => $url ,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => http_build_query( $post_data ),
    CURLOPT_HTTP_VERSION => 1.0,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HEADER => false
);

$curl = curl_init();

curl_setopt_array( $curl, $curl_options );

$result = curl_exec( $curl );

//echo $result;

function jsonp_decode($jsonp, $assoc = false) { // PHP 5.3 adds depth as third parameter   to json_decode
   if($jsonp[0] !== '[' && $jsonp[0] !== '{') { // we have JSONP
       $jsonp = substr($jsonp, strpos($jsonp, '('));
    }
    return json_decode(trim($jsonp,'();'), $assoc);
}
echo "<pre>";
var_dump(jsonp_decode($result, true));
echo "</pre>";

switch (json_last_error()) {
    case JSON_ERROR_NONE:
        echo ' - No errors';
    break;
    case JSON_ERROR_DEPTH:
        echo ' - Maximum stack depth exceeded';
    break;
    case JSON_ERROR_STATE_MISMATCH:
        echo ' - Underflow or the modes mismatch';
    break;
    case JSON_ERROR_CTRL_CHAR:
        echo ' - Unexpected control character found';
    break;
    case JSON_ERROR_SYNTAX:
        echo ' - Syntax error, malformed JSON';
    break;
    case JSON_ERROR_UTF8:
        echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
    break;
    default:
        echo ' - Unknown error';
    break;
   }

   echo PHP_EOL;

   foreach($result['VIDEOS'] as $p)
   {
     echo '

     Lesson Info: '.$p['LESSONTEXT'].' '.$p['LESSONID'].'

     Image Link: '.$p['THUMBNAIL'].'

     ';
   }

我返回的 JSON

array(3) {
  ["frame_size"]=>
  array(2) {
    ["width"]=>
    int(800)
    ["height"]=>
    int(470)
  }
  ["num_results"]=>
  int(9)
  ["VIDEOS"]=>
  array(9) {
    [0]=>
    array(8) {
      ["LESSONTEXT"]=>
      string(29) "test again... : test again..."
      ["LESSONID"]=>
      string(4) "4877"
      ["INSTRUCTORID"]=>
      string(3) "193"
      ["NOTE"]=>
      string(21) "I analyzed your video"
      ["SHAREDBY"]=>
      string(3) "193"
      ["LESSONDATE"]=>
      string(10) "1376601089"
      ["VIDEOURL"]=>
      string(100) "http://www.swingacademy.com/web/external/pages /apiPlayer.aspx?l=21E7D7B4-822D-4693-8C66-62C759D669E5"
      ["THUMBNAIL"]=>
      string(90) "http://www.swingacademy.com/useravatars/SA/SKILLCAST_SNAP/000001-100000/4001-5000/4877.jpg"
    }
    [1]=>
    array(8) {
      ["LESSONTEXT"]=>
      string(25) "test lesson : test lesson"
      ["LESSONID"]=>
      string(4) "4876"
      ["INSTRUCTORID"]=>
      string(3) "193"
      ["NOTE"]=>
      string(21) "I analyzed your video"
      ["SHAREDBY"]=>
      string(3) "193"
      ["LESSONDATE"]=>
      string(10) "1376601043"
      ["VIDEOURL"]=>
      string(100) "http://www.swingacademy.com/web/external/pages/apiPlayer.aspx?l=BE475D77-BAD8-4460-8729-83AE67220540"
      ["THUMBNAIL"]=>
      string(90) "http://www.swingacademy.com/useravatars/SA/SKILLCAST_SNAP/000001-100000/4001-5000/4876.jpg"
    }
    [2]=>
    array(8) {
      ["LESSONTEXT"]=>
      string(14) "test : testing"
      ["LESSONID"]=>
      string(4) "4875"
      ["INSTRUCTORID"]=>
      string(3) "193"
      ["NOTE"]=>
      string(21) "I analyzed your video"
      ["SHAREDBY"]=>
      string(3) "193"
      ["LESSONDATE"]=>
      string(10) "1376598912"
      ["VIDEOURL"]=>
      string(100) "http://www.swingacademy.com/web/external/pages/apiPlayer.aspx?l=C9755920-9B7D-46D3-97DE-E52DA41966DC"
      ["THUMBNAIL"]=>
      string(90) "http://www.swingacademy.com/useravatars/SA/SKILLCAST_SNAP/000001-100000/4001-5000/4875.jpg"
    }
    [3]=>
    array(8) {
      ["LESSONTEXT"]=>
      string(16) "this is a test :"
      ["LESSONID"]=>
      string(4) "4874"
      ["INSTRUCTORID"]=>
      string(4) "3795"
      ["NOTE"]=>
      string(21) "I analyzed your video"
      ["SHAREDBY"]=>
      string(4) "3795"
      ["LESSONDATE"]=>
      string(10) "1375361193"
      ["VIDEOURL"]=>
      string(100) "http://www.swingacademy.com/web/external/pages/apiPlayer.aspx?l=5B3E4DE3-AD09-4FEE-B111-937D4664B844"
      ["THUMBNAIL"]=>
      string(0) ""
    }
    [4]=>
    array(8) {
      ["LESSONTEXT"]=>
      string(50) "First review of myself : my first review of myself"
      ["LESSONID"]=>
      string(4) "4872"
      ["INSTRUCTORID"]=>
      string(5) "77360"
      ["NOTE"]=>
      string(21) "I analyzed your video"
      ["SHAREDBY"]=>
      string(5) "77360"
      ["LESSONDATE"]=>
      string(10) "1375306891"
      ["VIDEOURL"]=>
      string(100) "http://www.swingacademy.com/web/external/pages/apiPlayer.aspx?l=0FDBE5F0-0A6E-43FF-A4CB-4320D2E6C043"
      ["THUMBNAIL"]=>
      string(0) ""
    }
    [5]=>
    array(8) {
      ["LESSONTEXT"]=>
      string(25) "test record : test record"
      ["LESSONID"]=>
      string(4) "4871"
      ["INSTRUCTORID"]=>
      string(4) "3795"
      ["NOTE"]=>
      string(21) "I analyzed your video"
      ["SHAREDBY"]=>
      string(4) "3795"
      ["LESSONDATE"]=>
      string(10) "1374257704"
      ["VIDEOURL"]=>
      string(100) "http://www.swingacademy.com/web/external/pages/apiPlayer.aspx?l=FEA838AF-BB74-4478-9773-7CD3F160DF83"
      ["THUMBNAIL"]=>
      string(0) ""
    }
    [6]=>
    array(8) {
      ["LESSONTEXT"]=>
      string(8) "Test 1 :"
      ["LESSONID"]=>
      string(4) "4870"
      ["INSTRUCTORID"]=>
      string(4) "9924"
      ["NOTE"]=>
      string(21) "I analyzed your video"
      ["SHAREDBY"]=>
      string(4) "9924"
      ["LESSONDATE"]=>
      string(10) "1373540666"
      ["VIDEOURL"]=>
      string(100) "http://www.swingacademy.com/web/external/pages/apiPlayer.aspx?l=8AD8B9A1-DDA6-4E89-A9A6-2805988FB758"
      ["THUMBNAIL"]=>
      string(0) ""
    }
    [7]=>
    array(8) {
      ["LESSONTEXT"]=>
      string(9) "testing :"
      ["LESSONID"]=>
      string(4) "4869"
      ["INSTRUCTORID"]=>
      string(4) "3788"
      ["NOTE"]=>
      string(21) "I analyzed your video"
      ["SHAREDBY"]=>
      string(4) "3788"
      ["LESSONDATE"]=>
      string(10) "1373321906"
      ["VIDEOURL"]=>
      string(100) "http://www.swingacademy.com/web/external/pages/apiPlayer.aspx?l=A3F34283-00C4-47F0-951A-BD0E44C3E9F5"
      ["THUMBNAIL"]=>
      string(0) ""
    }
    [8]=>
    array(8) {
      ["LESSONTEXT"]=>
      string(39) "this is a test analysis : test analysis"
      ["LESSONID"]=>
      string(4) "4868"
      ["INSTRUCTORID"]=>
      string(4) "3788"
      ["NOTE"]=>
      string(21) "I analyzed your video"
      ["SHAREDBY"]=>
      string(4) "3788"
      ["LESSONDATE"]=>
      string(10) "1373321858"
      ["VIDEOURL"]=>
      string(100) "http://www.swingacademy.com/web/external/pages/apiPlayer.aspx?l=34DB255C-5C12-4A15-B09B-0F734F0032E9"
      ["THUMBNAIL"]=>
      string(0) ""
    }
  }
}

- No errors
Warning: Invalid argument supplied for foreach() in

要注意这是为 JavaScript 构建的,但我需要将它与 PHP 一起使用。所以我做了所有的转换和处理。我现在无法弄清楚我有这些数据如何在 foreach 循环中显示它并回显变量。

保存这些数据的方式对我来说看起来很陌生。

4

2 回答 2

2
echo "<pre>";
var_dump(jsonp_decode($result, true));
echo "</pre>";
$arrResult = jsonp_decode($result, true);
.
.
.
foreach($arrResult['VIDEOS'] as $p)
于 2013-08-24T23:48:28.157 回答
1

在使用它之前,您需要将结果分配给jsonp_decode变量:$result

$result = jsonp_decode($result, true);
于 2013-08-24T23:48:45.500 回答