当前位置: 首页 > 知识库问答 >
问题:

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

弘靖琪
2023-03-14

我用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 )

答复:

{
    "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": [
        [],
        [],
        [],
        [],
        [],
        [],
        [],
        [],
        [],
        [],
        [],
        []
    ] }

请注意最后“数据”键中的空数组。

我在postman和jQuery中尝试了同样的请求。它返回正确的数据,没有任何损失。下面附有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会丢失一些数据(数据键数组)以及如何保留它?

共有1个答案

陶涵育
2023-03-14

你的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进行了两次编码。

 类似资料:
  • 如何从Jackson在中生成的json响应中排除所有空json对象、数组或空对象的数组? 我正在为一个客户端构建一个Spring Boot REST API。API向数据库发送请求,并且必须生成JSON响应。 DAO层发送本机SQL查询,并以列表形式接收巨大的DB结果。开发人员必须使用索引手动将此结果映射到Java对象(参见下面的代码) SQL查询返回大量空值(这些查询不能修改)。由于这些空值,只

  • 问题内容: 我正在尝试使用gson将json字符串转换为对象。 我在下面有一个非常简单的示例,它可以运行,但是结果答案为空,即:我的Answer对象的文本字段为空。 问题答案: 因为您的JSON与您的课程不符。 现在,您的JSON是一个对象数组,每个对象包含一个对象作为字段。 JSON的处理方式如下所示: 编辑以添加评论: 如果您无法更改输出,则需要一个“包装器”。就像是: 并使用这些数组。这就是

  • 问题内容: 在一个我目前正在从事的项目中,我遇到了一个角度异常: 在寻找解决方案的搜索中,我直接在浏览器中输入了Web服务的URL,但令人惊讶的是,我没有收到预期的数组。 Web服务类: 当我输入网址时,我希望看到带有JSON对象的JSON数组: 但是,相反,我收到的JSON对象的属性与我期望的JSON对象相同,没有任何数组: 所以我想知道为什么没有数组,当我添加另一个Clazz对象时会发生什么。

  • 问题内容: 我是后端代码的新手,我正在尝试创建一个将对我的JSON字符串进行响应的函数。我目前有一个例子 这基本上只是打印字符串“应该以JSON形式出现的随机数”。我要执行的操作是使用任何数字的JSON字符串进行响应。我需要放置其他内容类型吗?该函数应该将该值传递给客户端的另一个用户吗? 谢谢你的帮助! 问题答案: 在Express中使用res.json: 或者:

  • 来自API的Im有两个不同的响应。然后我判断对象,但是我如何将消息的内容返回到字符串呢?下面的示例返回Exception: Json响应: Json响应: 模型: