【问题标题】:Curl iteratively pass multiple payloads from json or arrayCurl 从 json 或数组中迭代地传递多个有效载荷
【发布时间】:2021-09-13 16:45:19
【问题描述】:

如何将多维关联数组或 json 作为有效负载传递给 cURL 请求。一段时间以来,我一直在尝试解决这个问题,但没有任何成功。

这是我到目前为止所做的。我发出一个 curl 请求并将一个 json 传递给 CURLOPT_POSTFIELDS 字段。这工作正常。问题是我希望代码为保存在变量中的 json 字符串中的多个用户运行

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://test.url/tokens',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS =>'{
    "customerName": "orland",
        "mno": "Network",
        "amount": "1",
        "msisdn": "447911123456",
        "description": "Awaiting",
        "reference": "0fgdufgdfgdfs" 
}',
  CURLOPT_HTTPHEADER => array(
    'Content-Type: application/json',
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

上面的代码运行良好。

CURLOPT_POSTFIELDS 的有效负载不固定。 所以这就是我尝试做的,我对 json 字符串进行编码并尝试将其传递给 for 循环。我确信这种方法还不错,但我的语法不太正确。这是我的代码。

<?php
$recip = '[{
        "customerName": "Sorland",
        "mno": "Network",
        "amount": "1",
        "msisdn": "447911123346",
        "description": "Awaiting",
        "reference": "0fgdufgdfgdfs" 
    },
    {
        "customerName": "Corland",
        "mno": "MTN",
        "amount": 1,
        "msisdn": "447911123678",
        "description": "Awaiting",
        "reference": "0jsbfbsubfhbj" 
    },
    {
        "customerName": "orland",
        "mno": "MTN",
        "amount": 1,
        "msisdn": "447911123111",
        "description": "Awaiting",
        "reference": "1234568djnfjnfjds" 
}]';

    $curl = curl_init();
    
    curl_setopt_array($curl, array(
          CURLOPT_URL => 'https://test.url.com/api/',
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_ENCODING => '',
          CURLOPT_MAXREDIRS => 10,
          CURLOPT_TIMEOUT => 0,
          CURLOPT_FOLLOWLOCATION => true,
          CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST => 'POST',
          CURLOPT_POSTFIELDS =>
    echo ("'{<br>");
    foreach($arr[$keys[$i]] as $key => $value) {

        echo implode($key . " : " . $value . ",<br>");
    }
    echo "}',<br>";
        CURLOPT_HTTPHEADER => array(
        'Content-Type: application/json',
      ),
    ));

    $response = curl_exec($curl);

    curl_close($curl);
    echo $response;
}

我想使用 for 循环来发出具有不同有效负载的多个 curl 请求,并且有效负载来自上面的 json 数据。我将 json 转换为一个数组,以便能够循环并将其转换回 json 用于CURLOPT_POSTFIELDS

【问题讨论】:

  • 你为什么要使用 JSON 字符串,将其转换为 PHP 数组 .,.... 只是为了将其转换回 JSON 字符串...或者我错过了什么
  • 好的,我知道了@RiggsFolly。我正在关注一些教程并最终完成了。请问您建议我如何进行?
  • 不太确定您要做什么,但是将所有 4 个其他数组添加到 JSON 数据结构中会使 json 无效
  • 好的,如果您不确定我要达到的目标,那么也许我应该重新提出这个问题。我的第一个代码使用有效负载进行 curl 调用。我只是想多次拨打 curl 电话。但有效载荷将来自 json。
  • @RiggsFolly,我刚刚编辑了这个问题。我希望现在更清楚了

标签: php arrays json curl


【解决方案1】:

这似乎是一种更简单的方法来多次调用 curl 并保存响应,希望这次我得到了正确的结果:)

$recip = 
    '[
        {"customerName": "Sorland", "mno": "Network", "amount": "1",
        "msisdn": "447911123346","description": "Awaiting","reference": "0fgdufgdfgdfs" 
        },
        {"customerName": "Corland","mno": "MTN","amount": 1,
         "msisdn": "447911123678","description": "Awaiting","reference": "0jsbfbsubfhbj" 
        },
        {"customerName": "orland","mno": "MTN","amount": 1,"msisdn": "447911123111",
         "description": "Awaiting","reference": "1234568djnfjnfjds" 
        }
    ]';


// make a PHP datatype out of the JSON so you can use it

$recipients = json_decode($recip);

$responses = [];

foreach ( $recipients as $r) {
   
    $curl = curl_init();
    // convert $r back to json so it can be passed as param
    $postfield = json_encode($r);
    
    curl_setopt_array($curl, array(
        CURLOPT_URL => 'https://test.url.com/api/',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'POST',
        CURLOPT_POSTFIELDS => $postfield,
        CURLOPT_HTTPHEADER => array('Content-Type: application/json')
    ));

    $response[] = curl_exec($curl);
    // if you need to know which customer the response was for 
    // use this next line instead, for example
    //$response[$r->customerName] = curl_exec($curl);

    curl_close($curl);
}

// this is now an array of all responses
print_r($responses);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-07
    • 2021-05-26
    • 1970-01-01
    • 2019-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-10
    相关资源
    最近更新 更多