0

だから私はこれを無駄に働かせようとしました。以下は、PHP ファイルと JSON データの両方です。

最初に JSONP を JSON に変換する必要があることに注意してください。それはうまくいきました。このクレイジーなデータの配列を見ると、foreach ループで変数を取得する方法がわかりません。どんな助けも良い助けになります。

ありがとう

MYFILE.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 に答える