簡體   English   中英

使用 php 數組為 CURL 表單數據創建表單數據

[英]Create Form Data using php array for CURL Form data

我有一些使用 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 可以用嵌套數組寫入嗎?? 如何使類/對象變量/指針之類的東西可能是,所以我們有數據數組和自己的值。 請有人幫忙

答案應該是這樣的。

$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); 

那這個呢?

$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));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM