I want to change the response of an API if an incorrect value is typed..
<?php
if(isset($_GET['q']))
{
$q = ($_GET['q']);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.me.co/bank/resolve?account_number=".$q."&bank_code=".$bcode."",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer demo_03a647487df8a50d343aeb66d2a7305yhfkfllba",
"Cache-Control: no-cache",
),
));
$response = curl_exec($curl);
$err =
curl_error($curl);
curl_close($curl);
}
?>
Now $response is the ouput and irrespective of wat input the user types the $response will ouptut something. If the users input is wrong, it will show a php error which will scare the user.
$q is the users input and have tried using something like
if(!$response)
{
echo "incorrect";
}
The only thing that worked was turning off the error reporting for that page which i do not want cos i dont want the user to proceed.
In summary what i want is to customise the error from $response if the user input is wrong. Thanks as usual.
Related
I use postman for the first time.
My goal is to download a JSON file.
I managed to look at the code for cURL in postman:
But if I test the code in the command line, I can not log in.
Actually, I want to call the JSON file via PHP. I have tested initial approaches. In addition to the problem that I can not authenticate myself here, I think that I need more values. For example POSTFIELDS. Is that correct and if so, how do I find what I have to enter in Postman? Here is my initial Code:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.divessi.com/divessi/index.php",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "..",
CURLOPT_HTTPHEADER => array(
"Authorization: Basic BASE64",
"Cache-Control: no-cache",
"Postman-Token: TOKEN",
"content-type: ??"
),
));
$response = curl_exec($curl);
$obj = json_decode($response);
$events = array();
if ($obj)
{
$events = $obj[0]->DATA;
}
$err = curl_error($curl);
curl_close($curl);
if ($err)
{
echo "cURL Error #:" . $err;
} else {
foreach ($events as $event) {
...
}
I am making a API request in my web app which looks like this:
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://******.com/jdconnectionpool/view?
requestAction=JOB_FILE_DETAILS&job_no=7476709",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"accept: application/json",
"cache-control: no-cache"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
Everything looks to be fine. However, it returns null sometimes for the same set of params on calling multiple times. I do not seem to find any pattern either. Any far fetched information from someone who has experienced this phenomenon would be really appreciated.
I'm working with an API which is made by a classmate. I used Postman to generate the PHP cURL for the connection, with Authorization Basic. This works perfectly. Now, I want to get rid of the Authorization Basic and use my own API Key & Secret (password). Are there any good ways to do this?
Thanks in advance!
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "Some link here",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"authorization: Basic //something here",
"cache-control: no-cache",
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$result = json_decode($response,true);
}
I am making a request to an API using its post.rest web service. I have constructed the request using Postman. I get the error message, "Parameter 'query' is required, and it must have a value." I have been trying to work this out for several days, and I'm at my wits' end. I thought I could resolve it myself, but have not been successful. It seems like I've tried at least a hundred different permutations of the code.
Here is my PHP code for the request:
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://SDMDataAccess.sc.egov.usda.gov/Tabular/post.rest",
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 = array("query" => "SELECT l.areasymbol, l.areaname, l.lkey,
musym, muname, museq, mu.mukey
FROM sacatalog sac
INNER JOIN legend l ON l.areasymbol = sac.areasymbol
AND l.areatypename = 'Non-MLRA Soil Survey Area'
INNER JOIN mapunit mu ON mu.lkey = l.lkey
AND mu.mukey IN (455997)
", "FORMAT" => "JSON"),
CURLOPT_HTTPHEADER => array(
"Cache-Control: no-cache",
"Content-Type: application/json",
"Postman-Token: 520636bc-a062-4ab8-99e7-7edaae5118b4"
),
));
file_put_contents('soil_request.txt', $data);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
I am unable to find any examples of code for submitting rest requests to this API service in their documentation or elsewhere on the internet, even though their documentation is vast.
Thank you in advance for your consideration on this. I really appreciate it.
From https://en.1answer.info/676973-7a313939383734 it looks like you may need to change your data array into a json string before sending it
Also, your line that is
CURLOPT_POSTFIELDS=>$data=array("select ....
is wrong.
Instead, define your data array, then build a http query string out of it. Also, the parameter names are case sensitive, so you want to use query instead of QUERY and format instead of FORMAT.
Working code -
<?php
$data["query"]="SELECT l.areasymbol, l.areaname, l.lkey,
musym, muname, museq, mu.mukey
FROM sacatalog sac
INNER JOIN legend l ON l.areasymbol = sac.areasymbol
AND l.areatypename = 'Non-MLRA Soil Survey Area'
INNER JOIN mapunit mu ON mu.lkey = l.lkey
AND mu.mukey IN (455997)";
$data["format"] = "JSON";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://SDMDataAccess.sc.egov.usda.gov/Tabular/post.rest",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => http_build_query($data),
CURLOPT_HTTPHEADER => array(
"Cache-Control: no-cache",
"Content-Type: application/json",
"Postman-Token: 520636bc-a062-4ab8-99e7-7edaae5118b4"
),
));
file_put_contents('soil_request.txt', $data);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
Hey,
i am trying to only show 1 bit of the long string i get from the api i got fram postman, the only thing i need to show is the city. How do i need do this?
i'm trying to find a way with php but i have no clue what to do
a:14:{s:10:"regionName";s:10:"California";s:6:"status";s:7:"success";s:4:"city";s:13:"Mountain View";s:8:"timezone";s:19:"America/Los_Angeles";s:7:"country";s:13:"United States";s:11:"countryCode";s:2:"US";s:3:"zip";s:0:"";s:3:"lon";d:-122.08499908447266;s:3:"isp";s:6:"Google";s:2:"as";s:19:"AS15169 Google Inc.";s:5:"query";s:7:"8.8.8.8";s:6:"region";s:2:"CA";s:3:"lat";d:37.42290115356445;s:3:"org";s:6:"Google";}
(im using the ip of google just for this question)
so the length of the city name changes!
the site where i got it frm http://ip-api.com/php/8.8.8.8
and the code i am using:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://ip-api.com/php/8.8.8.8",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"postman-token: 2e83e542-a6fb-5bb6-94e0-c1908282a2a2"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
That's a product of running a PHP variable in a PHP serialize you can reverse it with unserialize
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://ip-api.com/php/8.8.8.8",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"postman-token: 2e83e542-a6fb-5bb6-94e0-c1908282a2a2"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
$responseArray = unserialize($response); //You probably need some error trapping here
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $responseArray["country"];
}