repl.it
@dhruvdutt/

Appbaseio - PHP - Bulk - Index Data

PHP (Legacy)

Bulk request including the index method and all data objects

fork
loading
main.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
$curl = curl_init();

// Array of data objects
$dataArray = array(
  array(
    "id" => "X1",
    "department_name" => "Books",
    "department_name_analyzed" => "Books",
    "department_id" => 1,
    "name" => "A Fake Book on Network Routing",
    "price" => 1032,
  ),
  array(
    "id" => "X2",
    "department_name" => "Books",
    "department_name_analyzed" => "Books",
    "department_id" => 2,
    "name" => "A Fake Book on Computer Science",
    "price" => 4125,
  ),
  array(
    "id" => "X3",
    "department_name" => "Books",
    "department_name_analyzed" => "Books",
    "department_id" => 3,
    "name" => "A Fake Book on Reactive Programming",
    "price" => 1337,
  )
);

$requestBody = "";

// Creating request body
for ($i = 0; $i < sizeof($dataArray); $i++) {
  // Pushing the operation and _id for each request
  $type = json_encode(array(
    "index" => array(
      "_id" => $dataArray[$i]["id"]
    )  
  ));
  $data = json_encode($dataArray[$i]);
  $requestBody .= $type."\n".$data."\n";
}

// Bulk request including the index method and all data objects
curl_setopt_array($curl, array(
  CURLOPT_URL => "https://scalr.api.appbase.io/newstreamingapp/books/_bulk",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => $requestBody,
  CURLOPT_HTTPHEADER => array(
    "Authorization: Basic bWVxUmY4S0pDOjY1Y2MxNjFhLTIyYWQtNDBjNS1hYWFmLTVjMDgyZDVkY2ZkYQ==",
    "Content-Type: application/x-ndjson"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  $json = json_decode($response, true);
  print_r("@bulk success: ");
  print_r($json);
}
PHP 7.0.8 (Legacy: use the following for new features: Command-line PHP: https://repl.it/languages/php_cli PHP Web Server: https://repl.it/languages/php7