-1

I have below code & I am generating tracking_id values manually & its working fine :

<?php

$data = [
"client_reference_id" => "ABCD",
"tracking_id" => "1234",
];
$data = json_encode($data);

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$curl_response = curl_exec($curl);
curl_close($curl);
echo $curl_response ."\n";

Result :

{"response":[{"tracking_id":"1234",}],

Now i need to create tracking_id dynamically.... so i tried like below :

"tracking_id" => "$r = 'DOCC'. mt_rand(0000000001,9999999999); echo $r;",

I got below Result :

{"response":[{"tracking_id":" = 'DOCC'. mt_rand(0000000001,9999999999); echo ;","

But i should get some random number as tracking_id....

Means php code inside parameter is not working....

  • 1
    Are you just looking to set a specific key in an array? Don't enclose it all in quotes then: `"tracking_id" => 'DOCC' . mt_rand(0000000001,9999999999),` – h2ooooooo Nov 05 '18 at 13:14
  • 1
    Possible duplicate of [Concatenate PHP function output to a string like variables](https://stackoverflow.com/questions/27494038/concatenate-php-function-output-to-a-string-like-variables) – MonkeyZeus Nov 05 '18 at 13:14
  • @h2ooooooo sometimes it work, but sometime it give this message : `{"response":[{"tracking_id":"DOCC815327051","status":"REQUEST_REJECTED","message":["trackingId : Invalid tracking ID, Provided value: [DOCC815327051]"]}],` –  Nov 05 '18 at 13:24

3 Answers3

1

As your require 10 digit random number:

function randomNumber($length) {
$result = '';

for($i = 0; $i < $length; $i++) {
    $result .= mt_rand(1, 9);
}

return $result;
}

$data = [
"client_reference_id" => "ABCD",
"tracking_id" => 'DOCC'.randomNumber(10);//no need to echo, just assign it
];
$data = json_encode($data);

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$curl_response = curl_exec($curl);
curl_close($curl);
echo $curl_response ."\n";
Manpreet
  • 2,450
  • 16
  • 21
  • Do reduce the max value mt_rand to mt_rand(1111111,9999999) – Manpreet Nov 05 '18 at 13:23
  • sometimes it work, but sometime it give this message : `{"response":[{"tracking_id":"DOCC815327051","status":"REQUEST_REJECTED","message":["trackingId : Invalid tracking ID, Provided value: [DOCC815327051]"]}],` –  Nov 05 '18 at 13:23
  • check your api, it might only allow digits. replace with "tracking_id" => mt_rand(1111111,9999999); – Manpreet Nov 05 '18 at 13:25
  • no, it should be in this format only : `DOCC-ten digits` –  Nov 05 '18 at 13:26
  • do this: "tracking_id" => 'DOCC'.mt_rand(11111,99999).mt_rand(11111,99999); – Manpreet Nov 05 '18 at 13:28
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/183126/discussion-between-manpreet-and-vickey-colors). – Manpreet Nov 05 '18 at 13:29
0

Refactor your code:

$r = 'DOCC'. mt_rand(0000000001,9999999999);

$data = [
  "client_reference_id" => "ABCD",
  "tracking_id" => $r,
];
rjdown
  • 9,162
  • 3
  • 32
  • 45
  • Thanks, sometimes it work, but sometime it give this message : `{"response":[{"tracking_id":"DOCC815327051","status":"REQUEST_REJECTED","message":["trackingId : Invalid tracking ID, Provided value: [DOCC815327051]"]}],` –  Nov 05 '18 at 13:25
  • Finally This worked for me : 'DOCC'.mt_rand(11111,99999).mt_rand(11111,99999), –  Nov 05 '18 at 13:36
  • can you please tell me how to save that tracking number in database ? –  Nov 05 '18 at 13:45
  • It is best to ask a new question, but here's an example https://stackoverflow.com/questions/18655706/pdo-with-insert-into-through-prepared-statements – rjdown Nov 05 '18 at 23:31
0

Just replace $data array with the following.

$data = [
"client_reference_id" => "ABCD",
"tracking_id" => 'DOCC'.  mt_rand(0000000001,9999999999),
];
Thomas Urban
  • 4,649
  • 26
  • 32
Chandra Sekar
  • 302
  • 1
  • 13