Pass Number Field As An Array - php

I am using a online laravel api doc to execute a program. When i run my code in postman, i get the error "pass number field as an array". But the number field is being already passed as an array field. What could i be missing below in my code ? Thanks in advance
public function testAPI(Request $request)
{
$on_call_back = 'https://learntoday.co.uk/var';
$id = '*****';
$url = $on_call_back.'?key='.$id;
$variables = [
'number' => ['44234200234,44234242002'],
'from' => 'world',
'content' => 'I love to code',
];
$ch = curl_init();
$headers = array();
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($variables));
$result = curl_exec($ch);
$result = json_decode($result, TRUE);
return $data;
curl_close($ch);
}
When i print out my results, this is what i see
Array
(
[number] => Array
(
[0] => '44234200234,44234242002'
)
[from] => world
[content] => i love to code
)
{"status":"error","message":"Pass number field as an array"}
Here is the update of how it appears now
Array
(
[recipient] => Array
(
[0] => 44234200234
[1] => 44234242002
)
[from] => world
[content] => i love to code
)

You should try this:
public function testAPI(Request $request)
{
$on_call_back = 'https://learntoday.co.uk/var';
$id = '*****';
$url = $on_call_back.'?key='.$id;
$variables = [
'number' => ['44234200234','44234242002'],
'from' => 'world',
'content' => 'I love to code',
];
$ch = curl_init();
$headers = array();
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($variables));
$result = curl_exec($ch);
$result = json_decode($result, TRUE);
return $data;
curl_close($ch);
}

You should try passing numbers on different indexes of array:
$variables = [
'number' => ['44234200234','44234242002'],
'from' => 'world',
'content' => 'I love to code',
];
Currently you are passing both numbers separated with , on 0 index.

Try Like This :
$numbersArray = array("44234200234","44234242002");
$variables = [
//'number' => ['44234200234','44234242002'],
'number' => $numbersArray,
'from' => 'world',
'content' => 'I love to code',
];
Ideally Old solution should work but you can try like this and check it.

You are passing your array as 1 string. To achieve with this (incorrect) approach, you would need to seperate the strings by a comma, and then convert each string to an integer.
'number' => ['44234200234,44234242002,1,2,3,4'], // this is ONE string
'number' => ['1', '2', '3', '4'] // this is many strings
What you want to do is pass an array of integers
numbers => [1, 2, 3]
Happy coding :-)

Try to use serialize function:
$on_call_back = 'https://learntoday.co.uk/var';
$id = '*****';
$url = $on_call_back.'?key='.$id;
$variables = [
'number' => serialize(['44234200234','44234242002']),
'from' => 'world',
'content' => 'I love to code',
];

Related

PHP CURL Open Ai Remove The Quotes From Post Data

Below is my PHP Curl script that works to post to Open AI, this is all working as it should.
I want to be able to set the values from posted data like this.
$getOpenAITemperature = $_POST[OpenAITemperature];
$getmaxtokens = $_POST[OpenAIMaximumLength];
$getTopp = $_POST[OpenAITopP];`
But when I do it added quotes to the posted values and it stops working.
Like this.
$postData = [
'model' => $getOpenAIModel,
'prompt' => $getRequest,
'temperature' => "0.24",
'max_tokens => "250",
'top_p' => "1",
But it needs to look like this to work.
$postData = [
'model' => $getOpenAIModel,
'prompt' => $getRequest,
'temperature' => 0.24,
'max_tokens => 250,
'top_p' => 1,
How can I remove the quotes around the numbers ? The quotes around the model and prompt are fine its just the numbers.
*** The script below here works fine ***
$getOpenAITemperature = 0.5;
$getmax_tokens = 250;
$gettop_p = 1;
$OPENAI_API_KEY = "sk-123";
$getOpenAIModel = "text-davinci-003";
$getRequest "My Question";
$ch = curl_init();
$headers = [
'Accept: application/json',
'Content-Type: application/json',
'Authorization: Bearer '.$OPENAI_API_KEY.''
];
$postData = [
'model' => $getOpenAIModel,
'prompt' => $getRequest,
'temperature' => $getOpenAITemperature,
'max_tokens' => $getTopp,
'top_p' => $getmaxtokens,
'best_of' => 2,
'frequency_penalty' => 0.0,
'presence_penalty' => 0.0,
'stop' => '["\n"]',
];
curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/completions');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));
$result = curl_exec($ch);`
I have tried a number of things like php trim() and str_replace but nothing worked.
You can cast the strings to an int or a float like so:
$postData['temperature'] = (float) $postData['temperature'];
$postData['max_tokens'] = (int) $postData['max_tokens'];
Check the PHP docs for type casting
https://www.php.net/manual/en/language.types.type-juggling.php#language.types.typecasting

cURL Redirect Returned URL

I want to send data from site A to site B. I've successfully transferred data but I want to redirect the url to the returned url generated from Curl. Below is my code.
$jsonData = array(
'first_name' => "$fname",
'last_name' => "$lname",
'phone_number' => "$phone",
'gender' => "$gender",
'email' => "$email",
'businessname' => "$businessname",
'natureofbusiness' => "$biznature",
'address' => "$address",
'utm' => "esg",
'date' => "$d"
);
$jsonDataEncoded = json_encode($jsonData);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch);
This is the result
{"data":{"success":true,"redirectUrl":"https://url.com/authorize
/eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1ODU5MDg4MzgsImRhdGEiOnsidXRtIjoiZXNnIiwiZW1haWwiOiJjbm5lYnVlNGFsbEB5YWhvby5jb20iLCJmaXJzdE5hbWUiOiJDaGlt
YSIsImxhc3ROYW1lIjoiT3NjYXIiLCJjb250YWN0TnVtYmVyIjoiOTg3MzczNjM3MyIsInRpbWVTdGFtcCI6IjE1ODU5MDc4NTUifSwiaWF0IjoxNTg1OTA4NTM4fQ.v6ecH7Tu5WB0ZkK-
U2ob_sQRSNn13rOU95Zo4BgwSF4?utm=esg","status":200}}
I need help to redirect to that Url.
Convert JSON result into array
$result = '{"data":{"success":true,"redirectUrl":"https://url.com/authorize/eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1ODU5MDg4MzgsImRhdGEiOnsidXRtIjoiZXNnIiwiZW1haWwiOiJjbm5lYnVlNGFsbEB5YWhvby5jb20iLCJmaXJzdE5hbWUiOiJDaGltYSIsImxhc3ROYW1lIjoiT3NjYXIiLCJjb250YWN0TnVtYmVyIjoiOTg3MzczNjM3MyIsInRpbWVTdGFtcCI6IjE1ODU5MDc4NTUifSwiaWF0IjoxNTg1OTA4NTM4fQ.v6ecH7Tu5WB0ZkK-U2ob_sQRSNn13rOU95Zo4BgwSF4?utm=esg","status":200}}';
$result = json_decode($result, true);
Now get url from array.
if(isset($result["data"])){
if($result["data"]["success"]==true){
$url = $result["data"]["redirectUrl"];
header("location:".$url);
}
}

Pass PhoneNumber field as an array

I am using an online laravel api documentation in my app. When i run my code in my browser, i get the error "pass phonenumber field as an array". But the number field is being already passed as an array field. What could i be missing below in my code ? Thanks in advance
public function testAPI(Request $request)
{
$on_call_back = 'https://learntoday.co.uk/var';
$id = '*****';
$url = $on_call_back.'?key='.$id;
$variables = [
'phoneNumber' => ['44234200234','44234242002'],
'from' => 'world',
'content' => 'I love to code',
];
$ch = curl_init();
$headers = array();
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($variables));
$result = curl_exec($ch);
$result = json_decode($result, TRUE);
curl_close($ch);
}
When i return $variables, i get the response
Array
(
[phoneNumber] => Array
(
[0] => 44234200234
[1] => 44234242002
)
[from] => test
[content] => I love to code
)
{"status":"error","message":"Make sure you are passing the phoneNumber field as an array"}
Your syntax of the array seems to be wrong, have you tried putting each number into single quotes like this:
'phoneNumber' => ['44234200234', '44234242002'],
Change
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($variables));
to
curl_setopt($ch, CURLOPT_POSTFIELDS, $variables);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($variables));
You can also do curl_setopt($ch, CURLOPT_POST, 1); instead of CURLOPT_CUSTOMREQUEST.
Please try send $variable as below code
$variables = array(
'phoneNumber' => array('44234200234','44234242002'),
'from' => 'world',
'content' => 'I love to code',
);
You should try this:
$variables = [
'phoneNumber' => ['44234200234','44234242002'],
'from' => 'world',
'content' => 'I love to code',
];
Updated answer
public function testAPI(Request $request)
{
$on_call_back = 'https://learntoday.co.uk/var';
$id = '*****';
$url = $on_call_back.'?key='.$id;
$variables = [
'phoneNumber' => ['44234200234','44234242002'],
'from' => 'world',
'content' => 'I love to code',
];
$ch = curl_init();
$headers = array();
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($variables));
$result = curl_exec($ch);
$result = json_decode($result, TRUE);
curl_close($ch);
}

not able to JSON encode an array

when trying to json_encode() an array, array not encoding
my array is :
$jsonData = array(
'addressId' => $addid,
'paymentMethod' => 2,
'transactionId' => $orderid
);
print_r($jsonData);
it prints :
Array ( [addressId] => 28 [paymentMethod] => 2 [transactionId] => ORDS63375431 )
and json encoding:
$jsonDataEncoded = json_encode($jsonData);
echo $jsonDataEncoded;
it prints :
{"addressId":"
what i am doing wrong?
here is my full code:
$addid = '<script>document.write($.session.get("addid"));</script>';
$orderid = $_POST["ORDERID"];
$accessToken = $_COOKIE['accessToken'];
$url = 'http://bookwise.co.in/app/api/carts/checkout';
$ch = curl_init($url);
$jsonData = array(
'addressId' => $addid,
'paymentMethod' => 2,
'transactionId' => $orderid
);
print_r($jsonData);
$jsonDataEncoded = json_encode($jsonData);
echo $jsonDataEncoded;
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','X-API-KEY: '.$accessToken.''));
$result = curl_exec($ch);

PHP - cURL To Array?

How could I use cURL to get back an array from this URL? http://module.game-monitor.com/67.202.102.136:27016/data/server.php
This is my code so far.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://module.game-monitor.com/67.202.102.136:27016/data/server.php');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_NOBODY, false); // remove body
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$head = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$head = unserialize($head);
$head = objectToArray($head);
$head["query_time"] = str_ireplace('ms', '', $head["query_time"]);
return $head;
Try just use PHP Type Casting (array) and remove the objectToArray function
$head = unserialize($head);
$head = (array) $head ;
var_dump($head);
Output
array
'ip' => string '67.202.102.136' (length=14)
'port' => string '27016' (length=5)
'player' => int 0
'maxplayer' => int 14
'name' => string 'Another www.cogameservers.com CSGO Server' (length=41)
'premium' => string '0' (length=1)
'link' => string 'http://www.game-monitor.com/csgo2_GameServer/67.202.102.136:27016/Another_www.cogameservers.com_CSGO_Server.html' (length=112)
'error' => int 0
'query_time' => string '0ms' (length=3)

Categories