I learning to use API from: https://rapidapi.com/lambda/api/face-recognition-and-face-detection/endpoints
I want to save or echo albumkey to variable and store in database. I have tried $response->albumkey and didn't work.
here my response below:
{
album: "amanda11",
msg: "Please put this in a safe place and remember it, you'll need it!",
albumkey: "c00bc5d3b1bf64a1ba68f690d4dabee494a2c6fbf48cf8f09c6d41fbece45b7b"
}
And here my code:
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://lambda-face-recognition.p.rapidapi.com/album",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "album=amanda11",
CURLOPT_HTTPHEADER => [
"content-type: application/x-www-form-urlencoded",
"x-rapidapi-host: lambda-face-recognition.p.rapidapi.com",
"x-rapidapi-key: 932571abf0msh45cf0f3cef74aacp19e151jsn33e9949a1974"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
if ($err) {
echo $err;
} else {
echo $response;
}
You need to parse the JSON before you can access its values. In PHP you do that with json_decode():
$response = curl_exec($curl);
$err = curl_error($curl);
if ($err) {
echo $err;
} else {
$json = json_decode($response);
echo $json->albumkey ;
}
Here's a basic example:
$response = '{
"album": "amanda11",
"msg": "Please put this in a safe place and remember it, you\'ll need it!",
"albumkey": "c00bc5d3b1bf64a1ba68f690d4dabee494a2c6fbf48cf8f09c6d41fbece45b7b"
}';
$json = json_decode($response);
echo $json->albumkey;
Demo
Related
I have to translate some information of my database to make PDF. I make a PHP function called "translate" that receive the strinf to be trtanslated by parameter. I have this API cURL code:
function translate($text)
{
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://microsoft-translator-text.p.rapidapi.com/translate?to=fr&api-version=3.0&from=pt&profanityAction=NoAction&textType=plain",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "[\r
{\r
\"Text\": \"I would really like to drive your car around the block a few times.\"\r
}\r
]",
CURLOPT_HTTPHEADER => [
"X-RapidAPI-Host: microsoft-translator-text.p.rapidapi.com",
"X-RapidAPI-Key: KEY",
"content-type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$json = json_decode($response);
echo $json[0]->translations[0]->text;
}
}
I want to replace:
CURLOPT_POSTFIELDS => "[\r
{\r
\"Text\": \"I would really like to drive your car around the block a few times.\"\r
}\r
]",
with:
$text
But i donĀ“t know how. Can you help me?
I am trying to send WhatsApp message to multiple numbers by using Ultramsg.com using PHP
by reference: https://docs.ultramsg.com/api/post/messages/chat
the code is like this:
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.ultramsg.com/instance1xxx/messages/chat",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "token=exxxxxxxx&to=1408XXXXXX1 #c.us,1408XXXXXX2 #c.us&body=WhatsApp API on UltraMsg.com works good&priority=10",
CURLOPT_HTTPHEADER => array(
"content-type: application/x-www-form-urlencoded"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
but I am getting this error
"to": "Wrong 'to' format for element (1408xxxxxxx #c.us,1408xxxxxxx #c.us). 'to' format : 14155552671#c.us or 14155552671-441234567890#g.us"
I am not understanding this.
I think should try to remove space before #c.us
I am working with Trip advisor Axesso Api , I want to format my response to json formatted format ,
this is the code I have used
<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://axesso-expedia-data-service.p.rapidapi.com/exp/expedia-lookup-reviews?page=2&propertyId=xxxxxx",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => [
"x-rapidapi-host: axesso-expedia-data-service.p.rapidapi.com",
"x-rapidapi-key: xxxxxxxxxxxxxxe"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo '<pre>';
echo $response;
echo '<pre>';
}
I have tried also
$response = json_encode($response, JSON_PRETTY_PRINT);
But none of them has worked,
Do anyone has an idea about this problem,
Thanks in advance
I'm not a PHP expert but here to help. I believe this should work:
$array = json_decode($response, true);
The data I want to post into the json api body below is from a while loop. So I want to Select from the database and send multiple students data from the database. I can post individually but I'm unable to do it from a loop. How please? Thanks.
{
"students":[
{
"admissionNumber": "2010",
"class":"js one"
},
{
"admissionNumber": "2020",
"class":"ss one"
}
],
"appDomain":"www.example.com"
}
Here's my code:
if(isset($_POST['submit'])){
$sql = "SELECT * FROM students";
while($row = mysqli_fetch_array($sql){
$admissionNumber = $row['admNo'];
$class = $row['class'];
$data = array('students' => array(['admissionNumber' => $admNo, 'class' => $class]),
'appDomain' => "http://example.com",
);
echo $data;
$url = 'https://the-end-point';
$params = $data;
$data_string = json_encode($data);
$curl = curl_init();
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $data_string,
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: application/json",
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
}
echo "<script>
alert(' Student Created Successfully!');
window.location = '.../students.php';
</script>";
}
}
}
I'm a total beginner and I don't know how to redirect to specific page after from success :
if ( !empty($errors)) {
$data['success'] = false;
$data['errors'] = $errors;
}
else{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.sendinblue.com/v3/contacts",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => '{"email":"'.$email.'", "updateEnabled":true, "listIds":[8], "attributes" : {"NOM":"'.$nom.'", "NIVEAU":"'.$niveau.'", "OPT-IN_METHOD":"FORMULAIRE"}}',
CURLOPT_HTTPHEADER => array(
"accept: application/json",
"api-key: KEY ",
"content-type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
}
$data['success'] = true;
$data['message'] = 'Success!';
}
echo json_encode($data);
}
I tried many solutions but I'm totally lost.
Thanks for your help