【问题标题】:Create Form Data using php array for CURL Form data使用 php 数组为 CURL 表单数据创建表单数据
【发布时间】:2016-03-30 09:45:30
【问题描述】:

我有一些使用 ajax 的请求,我想使用 curl 调用它。 那ajax表单数据应该是这样的

q:user_id(367091) {
  followed_by.after(10) {
    count,
    page_info {
      end_cursor,
      has_next_page
    },
    nodes {
      id,
      is_verified,
      followed_by_viewer,
      requested_by_viewer,
      full_name,
      profile_pic_url,
      username
    }
  }
}

我已经试着写了,有我的代码

$array_post= 
array(
    "q" => array(
        "" => "",
    ),
);
curl_setopt($c, CURLOPT_POSTFIELDS, $array_post);

我有点困惑填充喜欢 user_id,followed_by.after 可以用嵌套数组写吗? 如何使类/对象变量/可能是指针之类的东西,所以我们有数据数组和自己的值。请有人帮忙

【问题讨论】:

  • 我完全不明白这个问题。 “如何使类/对象像指针”和“具有数据数组和自己的值”是什么意思。价值不是数据吗?
  • 对不起我的英语不好。你们可以把它翻译成php中的数组吗?我的意思是,user_id 是数组吗? user_id value = 367091 并且它有数组元素数组是 follow_by.after ?并且,followed_by.after 的值 = 10,并且具有 count、arr page_info、arr 节点等元素。如果我错了,请纠正我。
  • 忘了提到@DawidFerenczy
  • 那个表单数据列表是什么的输出?
  • 我使用检查元素获取的 xhr。 @DawidFerenczy

标签: javascript php curl


【解决方案1】:

答案应该是这样的。

$array_post= 
array(
    "q" => "user_id(367091) {
  followed_by.after(10) {
    count,
    page_info {
      end_cursor,
      has_next_page
    },
    nodes {
      id,
      is_verified,
      followed_by_viewer,
      requested_by_viewer,
      full_name,
      profile_pic_url,
      username
    }
  }
}",
);
$field_string = urldecode(http_build_query($array_post));
curl_setopt($c, CURLOPT_POSTFIELDS, $field_string); 

【讨论】:

  • 谢谢。但是等等,你只需要一个硬编码的字符串,而不是任何结构,对吧?那么你到底在问什么?您只需照原样复制并粘贴您的示例请求。
  • 实际上我不知道它是否只能作为字符串,我只是尝试、错误、谷歌搜索,它可以正常工作。我认为 curl 应该是数组结构,但它应该以这样的方式工作。我一直在问的是在浏览器开发人员输入帖子数据中复制 xhr 的方式,所以我的网络服务可以像 @DawidFerenczy 那样做
【解决方案2】:

这个怎么样?

$test = array(
    'q:user_id(367091)' => array(
        'followed_by.after(10)' => array(
            'count' => '',
            'page_info' => array(
                'end_cursor' => '',
                'has_next_page' => ''
            ),
            'nodes' => array(
                'id' => '',
                'is_verified' => '',
                'followed_by_viewer' => '',
                'requested_by_viewer' => '',
                'full_name' => '',
                'profile_pic_url' => '',
                'username' => ''
            )
        )
    )
);

此输出看起来与您需要的相似:

print_r(json_encode($test, JSON_PRETTY_PRINT));

【讨论】:

  • 我得到错误字符串(48) "{"status":"fail","message":"invalid parameters"}"
  • 好的,在这种情况下,试着考虑一下。使用与以前相同的方法检查参数的外观并发布不同之处如何?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-30
  • 1970-01-01
  • 1970-01-01
  • 2012-11-22
  • 2012-06-08
  • 2020-04-05
  • 2022-11-11
相关资源
最近更新 更多