0

Need some advice. I am playing around with Mailinator PHP API and I was wondering if it is possible to fetch message count from multiple inboxes of Mailinator. May be using an array of inboxnames? I have tried it with single inbox and it fetches fine but when I use array, It gives me message count of first inbox (In array) only. Below is the code i've tried for fetching single inbox.

include 'Mailinator.php';
$mailinator = new Mailinator('API_KEY_HERE');
$data = new Inbox();
$data = $mailinator->inbox(exampleinbox);
echo $data->count();

I am looking for something like this, but doesn't work!

include 'Mailinator.php';
$inboxes = array("inbox1", "inbox2", "inbox3");
$mailinator = new Mailinator('API_KEY_HERE');
$data = new Inbox();
$data = $mailinator->inbox($inboxes);
echo $data->count(0);
echo $data->count(1);

It would also be great if i can make an array from file.txt

5
  • Have you considered a simple loop using the code that works Commented Apr 27, 2018 at 21:27
  • You mean something like this? stackoverflow.com/questions/36406463/… Commented Apr 27, 2018 at 21:28
  • No! I dont know Mailinator, but if a single pass works, why not wrap it in a foreach($inboxes as $inbox) and look at one at a time and then just add up the counts Commented Apr 27, 2018 at 21:30
  • It would be great if you can give me a demo code. In case you wish to check, I can give you api key Commented Apr 27, 2018 at 21:32
  • There you go, like I say its a bit of a guess so dont beat me up if I am wrong Commented Apr 27, 2018 at 21:37

2 Answers 2

0

Forgive me, I dont know Mailinator, but, if this code works

include 'Mailinator.php';
$mailinator = new Mailinator('API_KEY_HERE');
$data = new Inbox();
$data = $mailinator->inbox(exampleinbox);
echo $data->count();

Then to get the same data from multiple inboxes why not simply do

include 'Mailinator.php';
$mailinator = new Mailinator('API_KEY_HERE');

$inboxes = array("inbox1", "inbox2", "inbox3");
$counts = [];

foreach ($inboxes as $inbox) {
    $data = new Inbox();    
    $data = $mailinator->inbox($inbox);

    $counts[] = ['inbox'=>$inbox, 'count'=>$data->count()]
}

// here you can convert to JSON String if you like

echo json_encode($counts);
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot for writing this but I am sorry but I guess there is a misinterpretation. I don't need a count of all mailboxes, I just need counts of individual inboxes in an array or json or anything.
Is that more like what you want
Yes. Just that I need message counts from inboxes in array.
Well maybe give me a slightly bigger clue, as I believe they are in an array. so help me by describing the array you want
0

Yipee!! Got this working.

<?
// Created this Internal API Handler - api.php
$inbox = $_GET['inbox'];
include 'Mailinator.php';
$mailinator = new Mailinator('API_KEY_HERE');
$data = new Inbox();
$data = $mailinator->inbox($inbox);
$json = json_encode ($data->count());
echo "$json";
?>

Then used some multi curl!

<?php

//Filename multi.php

function multiRequest($data, $options = array()) {

  // array of curl handles
  $curly = array();
  // data to be returned
  $result = array();

  // multi handle
  $mh = curl_multi_init();

  // loop through $data and create curl handles
  // then add them to the multi-handle
  foreach ($data as $id => $d) {

    $curly[$id] = curl_init();

    $url = (is_array($d) && !empty($d['url'])) ? $d['url'] : $d;
    curl_setopt($curly[$id], CURLOPT_URL,            $url);
    curl_setopt($curly[$id], CURLOPT_HEADER,         0);
    curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1);

    // post?
    if (is_array($d)) {
      if (!empty($d['post'])) {
        curl_setopt($curly[$id], CURLOPT_POST,       1);
        curl_setopt($curly[$id], CURLOPT_POSTFIELDS, $d['post']);
      }
    }

    // extra options?
    if (!empty($options)) {
      curl_setopt_array($curly[$id], $options);
    }

    curl_multi_add_handle($mh, $curly[$id]);
  }

  // execute the handles
  $running = null;
  do {
    curl_multi_exec($mh, $running);
  } while($running > 0);


  // get content and remove handles
  foreach($curly as $id => $c) {
    $result[$id] = curl_multi_getcontent($c);
    curl_multi_remove_handle($mh, $c);
  }

  // all done
  curl_multi_close($mh);

  return $result;
}

?>

Then called this

<?php
require_once 'multi.php';
$data = array(
  'http://example.com/api.php?inbox=inbox1',
  'http://example.com/api.php?inbox=inbox2',
  'http://example.com/api.php?inbox=inbox3'
);
$r = multiRequest($data);

echo '<pre>';
print_r($r);

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.