2017-10-23 09:51
浏览 58


I am trying to make a request to an API. The problem is if there are more than 50 records in the result set. The API will break down the result into 'pages' of 50 records each. I am trying to get a single array that has all the results before I save into the database. Below is my code:

$url = 'http://developer-api.bringg.com/partner_api/users';

$data_string = array(
 'access_token' => "<YOUR ACCESS TOKEN>",
 'timestamp' => date('Y-m-d H:i:s'),
 'company_id' => <YOUR COMPANY ID>,
 'page' => 3 //this is the page key that needs to be specified

$secret_key = "<YOUR SECRET KEY>";

$signature = hash_hmac("sha1", http_build_query($data_string), 

$data_string["signature"] = $signature;

$content = json_encode($data_string);

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
curl_setopt($ch, CURLOPT_HTTPHEADER,
'Content-Length: ' . strlen($content))

$json_response = curl_exec($ch);

$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);

if ( $status != 200 ) {
 die("Error: call to URL $url failed with status $status, response 
   $json_response, curl_error " . curl_error($curl) . ", curl_errno " . 


$response = json_decode($json_response, true);

I can not know the number of records I have so I don't know how to dynamically change the page number. Is there a way I can do multiple requests and maybe change the page number with loops and then store all the results in one array. Please help, I am new to arrays and curl.

图片转代码服务由CSDN问答提供 功能建议

我正在尝试向API发出请求。 问题是结果集中是否有超过50条记录。 API会将结果分解为每个50条记录的“页面”。 我想在保存到数据库之前获得包含所有结果的单个数组。 下面是我的代码:

 $ url ='http://developer-api.bringg.com/partner_api/users';
nn$  data_string = array(
'access_token'=&gt;“&lt; YOUR ACCESS TOKEN&gt;”,
'timestamp'=&gt; date('Ymd H:i:s'),
'company_id'=&gt;&lt;  ;您的公司ID&gt;,
'page'=&gt; 3 //这是需要指定的页面密钥
 $ secret_key =“&lt; YOUR SECRET KEY&gt;”; 
 \  n $ signature = hash_hmac(“sha1”,http_build_query($ data_string),
 $ secret_key); 
 $ data_string [“signature”] = $ signature; 
 $ content = json_encode($ data_string);  
 $ ch = curl_init(); 
curl_setopt($ ch,CURLOPT_URL,$ url); 
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,1); 
curl_setopt($ ch,CURLOPT_CUSTOMREQUEST,“GET”); 
curl_setopt(  $ ch,CURLOPT_POSTFIELDS,$ content); 
curl_setopt($ ch,CURLOPT_HTTPHEADER,
array('Content-Type:application / json',
'Content-Length:'。strlen($ content))
); \  n 
 $ json_response = curl_exec($ ch); 
 $ status = curl_getinfo($ ch,CURLINFO_HTTP_CODE); 
if($ status!= 200){
 die(“错误: 调用URL $ url失败,状态为$ status,response 
 $ json_response,curl_error“。  curl_error($ curl)。  “,curl_errno”。  
curl_errno($ curl)); 
ncurl_close($ ch); 
 $ response = json_decode($ json_response,true); 

我不知道我的记录数量,所以我不知道如何动态更改页码。 有没有办法我可以做多个请求,也许可以用循环更改页码,然后将所有结果存储在一个数组中。 请帮助,我是阵列和卷曲的新手。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题