iT邦幫忙

0

php curl 多個?能夠怎麼寫?

$data = array(
    "manager" => $login_acc,
    "password" => $login_pas
  );
  $data_string = json_encode($data);
  $ch = curl_init('https://');
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array(
      'Content-Type: application/json',
      'Content-Length: ' . strlen($data_string))
  );
  curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
  $result = curl_exec($ch);
  curl_close($ch);
  $data = json_decode($result);

因為要處理http api,可能會有數十個
但每個都那麼長似乎不太好管理
想問說能怎麼樣簡寫才對?

把你的功能封裝成function就可以重覆利用了啊。
但是如果你想要的是這麼多的url資源能夠並行處理的話就抱歉了。
php不支援多執行緒所以無法同時對多個請求處理。
如果你的問題是後者,就考慮改用別的語言像是node.js或是python吧。
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答