数组中的对象为空 - php curl 响应

Objects are empty inside array - php curl response

我在 php-curl 中发出请求,响应中丢失了一些数据。 (数据数组填充的是空对象,此处数据丢失)

我的代码是:

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://api.vimeo.com/albums/5694909/videos?page=1&sort=manual&fields=description%252Cduration%252Cis_free%252Clive%252Cname%252Cpictures.sizes.link%252Cpictures.sizes.width%252Cpictures.uri%252Cprivacy.download%252Cprivacy.view%252Ctype%252Curi%252Cuser.link%252Cuser.name%252Cuser.pictures.sizes.link%252Cuser.pictures.sizes.width%252Cuser.uri&per_page=12&filter=&_hashed_pass=',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'GET',
  CURLOPT_HTTPHEADER => array(
    'Connection: keep-alive',
    'Accept: application/vnd.vimeo.video;version=3.4.1',
    'Origin: https://vimeo.com',
    'Authorization: jwt eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2Mzk0NzQ0NDAsInVzZXJfaWQiOjE1OTc1Nzk3NSwiYXBwX2lkIjo1ODQ3OSwic2NvcGVzIjoicHVibGljIHN0YXRzIHByaXZhdGUiLCJ0ZWFtX3VzZXJfaWQiOm51bGx9.6y_6aX_jChvbHpzK_72Y5SiHHrur44EEu1jh-1PZfn0',
    'Sec-Fetch-Dest: empty',
    'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36',
    'Content-Type: application/json',
    'Sec-Fetch-Site: same-site',
    'Sec-Fetch-Mode: cors',
    'Referer: https://vimeo.com/showcase/5694909?page=1',
    'Accept-Language: en-US,en;q=0.9'
  ),
));

$response = curl_exec($curl);
print_r(curl_getinfo($curl));
curl_close($curl);
echo $response;

这是输出:

curl_getinfo

Array (
     [url] => https://api.vimeo.com/albums/5694909/videos?page=1&sort=manual&fields=description%252Cduration%252Cis_free%252Clive%252Cname%252Cpictures.sizes.link%252Cpictures.sizes.width%252Cpictures.uri%252Cprivacy.download%252Cprivacy.view%252Ctype%252Curi%252Cuser.link%252Cuser.name%252Cuser.pictures.sizes.link%252Cuser.pictures.sizes.width%252Cuser.uri&per_page=12&filter=&_hashed_pass=
     [content_type] => application/vnd.vimeo.video+json
     [http_code] => 200
     [header_size] => 1036
     [request_size] => 1096
    [filetime] => -1
     [ssl_verify_result] => 0
     [redirect_count] => 0
     [total_time] => 0.875052
     [namelookup_time] => 0.000937
     [connect_time] => 0.060644
     [pretransfer_time] => 0.380411
     [size_upload] => 0
     [size_download] => 1401
     [speed_download] => 1601
     [speed_upload] => 0
     [download_content_length] => 1401
     [upload_content_length] => 0
     [starttransfer_time] => 0.875005
     [redirect_time] => 0
     [redirect_url] => 
     [primary_ip] => 151.101.64.217
     [certinfo] => Array
         (
         )
 
     [primary_port] => 443
     [local_ip] => 192.168.1.9
     [local_port] => 59793
     [http_version] => 2
     [protocol] => 2
     [ssl_verifyresult] => 0
     [scheme] => HTTPS
     [appconnect_time_us] => 380306
     [connect_time_us] => 60644
     [namelookup_time_us] => 937
     [pretransfer_time_us] => 380411
     [redirect_time_us] => 0
     [starttransfer_time_us] => 875005
     [total_time_us] => 875052 )

response :

{
    "total": 76,
    "page": 1,
    "per_page": 12,
    "paging": {
        "next": "/albums/5694909/videos?page=2&sort=manual&fields=description%252Cduration%252Cis_free%252Clive%252Cname%252Cpictures.sizes.link%252Cpictures.sizes.width%252Cpictures.uri%252Cprivacy.download%252Cprivacy.view%252Ctype%252Curi%252Cuser.link%252Cuser.name%252Cuser.pictures.sizes.link%252Cuser.pictures.sizes.width%252Cuser.uri&per_page=12&filter=&_hashed_pass=",
        "previous": null,
        "first": "/albums/5694909/videos?page=1&sort=manual&fields=description%252Cduration%252Cis_free%252Clive%252Cname%252Cpictures.sizes.link%252Cpictures.sizes.width%252Cpictures.uri%252Cprivacy.download%252Cprivacy.view%252Ctype%252Curi%252Cuser.link%252Cuser.name%252Cuser.pictures.sizes.link%252Cuser.pictures.sizes.width%252Cuser.uri&per_page=12&filter=&_hashed_pass=",
        "last": "/albums/5694909/videos?page=7&sort=manual&fields=description%252Cduration%252Cis_free%252Clive%252Cname%252Cpictures.sizes.link%252Cpictures.sizes.width%252Cpictures.uri%252Cprivacy.download%252Cprivacy.view%252Ctype%252Curi%252Cuser.link%252Cuser.name%252Cuser.pictures.sizes.link%252Cuser.pictures.sizes.width%252Cuser.uri&per_page=12&filter=&_hashed_pass="
    },
    "data": [
        [],
        [],
        [],
        [],
        [],
        [],
        [],
        [],
        [],
        [],
        [],
        []
    ] }

请注意末尾“data”键中的空数组。

我在 postman 和 jQuery 中尝试了相同的请求。它 returns 正确的数据没有任何损失。 Javascript 附在下面。

var settings = {
      "url": "https://api.vimeo.com/albums/5694909/videos?page=1&sort=manual&fields=description%2Cduration%2Cis_free%2Clive%2Cname%2Cpictures.sizes.link%2Cpictures.sizes.width%2Cpictures.uri%2Cprivacy.download%2Cprivacy.view%2Ctype%2Curi%2Cuser.link%2Cuser.name%2Cuser.pictures.sizes.link%2Cuser.pictures.sizes.width%2Cuser.uri&per_page=12&filter=&_hashed_pass=",
      "method": "GET",
      "timeout": 0,
      "headers": {
        "Connection": "keep-alive",
        "Accept": "application/vnd.vimeo.video;version=3.4.1",
        "Origin": "https://vimeo.com",
        "Authorization": "jwt eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2Mzk0NzQ0NDAsInVzZXJfaWQiOjE1OTc1Nzk3NSwiYXBwX2lkIjo1ODQ3OSwic2NvcGVzIjoicHVibGljIHN0YXRzIHByaXZhdGUiLCJ0ZWFtX3VzZXJfaWQiOm51bGx9.6y_6aX_jChvbHpzK_72Y5SiHHrur44EEu1jh-1PZfn0",
        "Sec-Fetch-Dest": "empty",
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36",
        "Content-Type": "application/json",
        "Sec-Fetch-Site": "same-site",
        "Sec-Fetch-Mode": "cors",
        "Referer": "https://vimeo.com/showcase/5694909?page=1",
        "Accept-Language": "en-US,en;q=0.9"
      },
    };
    
    $.ajax(settings).done(function (response) {
      document.write("data key contains :");
      document.write(JSON.stringify(response['data']));
    });</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

为什么 php 会丢失一些数据(数据键数组),我该如何保留它?

你的CURLOPT_URL错了。应该是:

  CURLOPT_URL => 'https://api.vimeo.com/albums/5694909/videos?page=1&sort=manual&fields=description%2Cduration%2Cis_free%2Clive%2Cname%2Cpictures.sizes.link%2Cpictures.sizes.width%2Cpictures.uri%2Cprivacy.download%2Cprivacy.view%2Ctype%2Curi%2Cuser.link%2Cuser.name%2Cuser.pictures.sizes.link%2Cuser.pictures.sizes.width%2Cuser.uri&per_page=12&filter=&_hashed_pass=',

你可能url编码了两次。