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
Related
To publish a plugin I must change my code to use the Wordpress HTTP Api.
Therefore, I have translate my code.
Before.
$data = array("Param1" => 'ValueParam1', "Param2" => "ValueParam2");
$data_string = json_encode($data);
$ch = curl_init($url);
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)
));
$result = curl_exec($ch);
After
$data = array("Param1" => 'ValueParam1', "Param2" => "ValueParam2");
$data_string = json_encode($data);
$result = wp_remote_post($url, array(
'headers' => array('Content-Type' => 'application/json'),
'body' => wp_json_encode($data_string),
'method' => 'POST',
'timeout' => 60, // added
'redirection' => 5, // added
'blocking' => true, // added
'httpversion' => '1.0',
'sslverify' => false,
));
But with the function wp_remote_post I have this problem.
When JSON data is sended to remote host, the data has the backslashes inside.
Example:
{\"Param1\":\"ValueParam1\","Param2\":\"ValueParam2\"}
What is wrong?
Have I wrong something in translate the origin code?
I need some help to send JSON data without these escaping characters.
Thanks.
Pass JSON_HEX_QUOT as a second parameter to wp_json_encode to escape the double quotes without a slash. See: https://developer.wordpress.org/reference/functions/wp_json_encode/ and https://www.php.net/manual/en/json.constants.php for more details.
I'm trying to make a Telegram Bot in PHP with a custom keyboard. The message is delivered, but the custom keyboard won't work. $keyb = array('keyboard' => array(array("A", "B"))); also no succes.
The sendMessage method referrers to ReplyKeyboardMarkup for the object. Making an array for ReplyKeyboardMarkup doesn't work. Also tried to json_encode($keyb) but that's also not the solution.
I searched in GitHub for examples but I haven't found one where the custom keyboard is used. Telegram runs on iPhone and desktop, both up-to-date.
Sample code:
$url = "https://api.telegram.org/bot<token>/sendMessage";
$keyb = array('ReplyKeyboardMarkup' => array('keyboard' => array(array("A", "B"))));
$content = array('chat_id' => <chat_id>, 'reply_markup' => $keyb, 'text' => "Test");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($content));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //fix http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/
// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
var_dump($server_output);
The docs seem to indicate you need to provide the reply_markup parameter as a JSON serialised object... kinda stupid for a form POST endpoint:
$replyMarkup = array(
'keyboard' => array(
array("A", "B")
)
);
$encodedMarkup = json_encode($replyMarkup);
$content = array(
'chat_id' => <chat_id>,
'reply_markup' => $encodedMarkup,
'text' => "Test"
);
Does this one work?
$keyboard = array(array("[Destaques]","[Campinas e RMC]","[esportes]"));
$resp = array("keyboard" => $keyboard,"resize_keyboard" => true,"one_time_keyboard" => true);
$reply = json_encode($resp);
$url = $GLOBALS[website]."/sendmessage?chat_id=".$chatId."&text=oi&reply_markup=".$reply;
file_get_contents($url);
This code works fine!
$keyboard = [
'keyboard' => [
[
['text' => 'hourly', 'callback_data' => 'Every hour'],
['text' => 'daily', 'callback_data' => 'Every day'],
['text' => 'weekly', 'callback_data' => 'Every week'],
['text' => 'monthly', 'callback_data' => 'Every month'],
['text' => 'yearly', 'callback_data' => 'Every year'],
] ]
];
this works for me
After creating about 1000 inventory items with code, Square suddenly started returning an error.
The error returned was:
{"type":"bad_request","message":"'name' is required"}
Example code:
$postData = array(
'id' => 'test_1433281486',
'name' => 'test',
'description' => 'test',
'variations' => array(
'id' => 'test_v1433281486',
'name' => 'Regular',
'price_money' => array(
'currency_code' => 'USD',
'amount' => '19800',
),
),
);
$json = json_encode($postData);
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPHEADER, array( 'Authorization: Bearer ' . $access_token, 'Content-Type: application/json', 'Accept: application/json'));
curl_setopt($curl, CURLOPT_URL, "https://connect.squareup.com/v1/me/items");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLINFO_HEADER_OUT, TRUE);
curl_setopt($curl, CURLOPT_VERBOSE, TRUE);
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
$json = curl_exec($curl);
curl_close($curl);
Here is the json_encoded string:
{
"id":"test_1433281486",
"name":"test",
"description":"test",
"variations": {
"id":"test_v1433281486",
"name":"Regular",
"price_money": {
"currency_code":"USD",
"amount":"19800"
}
}
}
I have tried everything I know to do a simple Create Item with code now, but it no longer works. Always returns the message "name" is required. My code to update images, fees, categories etc still works perfectly - just can't create.
I still have 100's of new inventory items to add, so getting this to work is imperative to business.
Variations needs to be passed in as an array. Here is how the JSON should look:
{
"id":"test_1433281486",
"name":"test",
"description":"test",
"variations": [{
"id":"test_v1433281486",
"name":"Regular",
"price_money": {
"currency_code":"USD",
"amount":"19800"
}
}]
}
Thanks for the report! We will update our error messaging to send the proper message if variations is not passed in as an array.
This is for all the PHP developers. Here is an updated Create Item PHP array that is compatible with Square. Notice the nested "variations" arrays.
Square now (6/15) requires brackets in JSON arrays - PHP json_encode() does not produce them unless you add nested arrays.
$postData = array(
'id' => 'test_1433281487',
'name' => 'test2',
'description' => 'test2',
'variations' => array(array(
'id' => 'test_v1433281487',
'name' => 'Regular',
'price_money' => array(
'currency_code' => 'USD',
'amount' => '19800',
),
)),
);
$json = json_encode($postData);
Here's another example of JSON brackets in PHP.
Here is the PHP json_encode() documentation.
I am fairly new to this so hopefully its not something silly i am missing out on. I am trying to create an order for someone using API.
I have searched for hours how to do this on stackoverflow and all the post have been great help. But now I have hit a brick wall. This code does not seem to throw any errors when visiting the PHP page nor does it take the item away from stock on like its meant to.
Am i missing something really simple, also is there a way to check if its working?
<?php
$today = date("D M j Y G:i:s T");
$data = array(
"order" => [
"order_number" => "",
"company_id" => 690094,
"billing_address_id" => 1018327,
"shipping_address_id" => 1018327,
"stock_location_id" => 16377,
"ship_at" => $today,
"issued_at" => $today,
"tax_type" => "exclusive",
"payment_status" => "unpaid",
"fulfillment_status" => "shipped",
"email" => null,
"reference_number" => 7784,
"status" => "fulfilled",
"order_line_items" => [
"quantity" => 1, "discount" => null, "price" => 1,
"tax_rate_override" => null, "freeform" => false,
"variant_id" => 6983077
],
],
);
$url ="https://api.tradegecko.com/orders/";
$str_data = json_encode($data);
function sendPostData($url, $str_data){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Accept: application/json',
'http' => array(
'header' => "Authorization: Bearer 872ed5e72dfc7e4e0adaa7c663cab7d81415078fdbe4f486d085b718c93b3eb3")
)
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$str_data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($ch);
curl_close($ch); // Seems like good practice
return $result;
}
echo sendPostData($url, $str_data);
?>
Setting the Authorization header is not done in the right way. It should be done as in:
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Accept: application/json',
'Authorization: Bearer 872ed5e72dfc7e4e0adaa7c663cab7d81415078fdbe4f486d085b718c93b3eb3')
);
For debugging purposes it is helpful to add:
curl_setopt($ch, CURLOPT_VERBOSE, 1);
Finally, you should create a new access token for this service since yours is public now.
All i know is company name and its country. Can anyone provide a sample php code. On Running this code I get an error property not found, "organisation email contact".
<?php
$service_url = 'https://www.googleapis.com/freebase/v1/search';
$params = array(
'query' => 'Emirates',
'key' => "My-Key",
'filter' => '(all type:/business/business_operation)',
"output" => '(/organization/email_contact )',
// "result" => array(
// "/organization/organization/email" => [],
// "name" => "Freebase Staff",
// "id" => "/business/business_operation"
// )
);
$url = $service_url . '?' . http_build_query($params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = json_decode(curl_exec($ch), true);
curl_close($ch);
print "<pre>";print_r($response);print "</pre>";
?>
Try changing the output field parameter value:
"output" => '(/organization/email_contact )' to "output" =>
'(all/organization/email_contact)'