Following is the coding I have done. But after posting the data, I am getting error as:
'message' => string ''from' and 'to' date must be given' (length=34).
Following is my code:
$auth_token=$_REQUEST['auth_token'];
$ch = curl_init('https://api.datonis.io/api/v3/datonis_query/thing_aggregated_data');
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array(
'X-Auth-Token:'.$auth_token,
'thing_key:6f2159f998',
'idle_time_required:true',
'from:2016/02/02 17:05:00',
'to:2016/08/29 17:10:00',
'time_zone:Asia/Calcutta',
'Content-Type:application/json',
),
));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Send the request
$response = curl_exec($ch);
// Check for errors
if($response === FALSE){
die(curl_error($ch));
}
// Decode the response
//var_dump($response);
$responseData = json_decode($response, TRUE);
// Print the date from the response
var_dump($responseData);
Actually I also want to get the data but the details are contained in the post request data.
use "CURLOPT_POSTFIELDS":
$data = array("from" => "2016/02/02 17:05:00", "to" => "2016/08/29 17:10:00");
$data_string = json_encode($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json')
Related
I succeeded in getting the contract log from infura with eth_getlogs.
But I don't know how to decode the retrieved data with PHP.
Please tell me how to decode retrieved data with PHP.
PHP code
$my_server_url = "https://mainnet.infura.io/v3/APIKey";
$ch = curl_init($my_server_url);
$data = array(
'jsonrpc' => '2.0',
'id' => 1,
'method' => 'eth_getLogs',
'params' => array(array(
"fromBlock" => "0x949986",
"toBlock" => "latest",
"topics" => ["0x70d79747edd06ece6bf0a5ee4429d0138fa4ccbeefaea4d21e0e511e9d81b094"],
"address" => "0xf0A0293D762aF2AC36E57613D42aC36773eEAf51"
))
);
$payload = json_encode($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
return $result;
Retrieved Data:
{"jsonrpc":"2.0","id":1,"result":[{"address":"0xf0a0293d762af2ac36e57613d42ac36773eeaf51","blockHash":"0x348b7e3dcf6c76c483f872851650b7aed8f2f6d6042207b8f55e13a6c52a19cd","blockNumber":"0x949986","data":"0x00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000973616d706c653132330000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e6372656174652073756363657373000000000000000000000000000000000000","logIndex":"0x85","removed":false,"topics":["0x70d79747edd06ece6bf0a5ee4429d0138fa4ccbeefaea4d21e0e511e9d81b094","0x0000000000000000000000008790dc53157f7090b85785fc107360a23ad63a13"],"transactionHash":"0x0b9544631e221ed9569aae363d272a54dc853cf7f84610bda3c828c6f181784e","transactionIndex":"0x4a"},{"address":"0xf0a0293d762af2ac36e57613d42ac36773eeaf51","blockHash":"0xc287eb38486a137e33a85f641efdcc1f57b90d3f7889709503e53fec54657ff0","blockNumber":"0x9499f2","data":"0x00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000973616d706c65313233000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000137365742061646472657373207375636365737300000000000000000000000000","logIndex":"0x60","removed":false,"topics":["0x70d79747edd06ece6bf0a5ee4429d0138fa4ccbeefaea4d21e0e511e9d81b094","0x0000000000000000000000008790dc53157f7090b85785fc107360a23ad63a13"],"transactionHash":"0xae01b5c746ce2229c566682208b7af1f0520e094dfc2adef69f22eb8dd6f3bf2","transactionIndex":"0x4a"}]}
I want to decode data section of retrieved data.
I would like to verify whether someone has a valid ISIC card, I have written the following code for this Rest API (http://nakoduj.to/_upload/project_files/2015-08-18-12-51-20_DM%20-%20Integration%20Manual.pdf), but it doesn't work, and I have no idea why it doesn't.
$data = array( "cardNumber" => "S123456789000A",
"cardholderName" => "John Doe");
$data = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://gts-dm.orchitech.net/api/verifications');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "testdm:testdm");
$result = curl_exec($ch);
if ($result === false) {
$info = curl_getinfo($ch);
curl_close($ch);
die('error occured during curl exec. Additioanl info: ' . var_export($info));
}
curl_close($ch);
I'm getting false for the $result always. And there is no additional information in $info.
Thank you in advance for your help
<?php
$data = [
"cardNumber" => "S123456789000A",
"cardholderName" => "John Doe",
];
$data = json_encode($data);
$header = $request_headers = [
"Content-Type: application/json"
];
$curl_options = [
CURLOPT_URL =>'https://gts-dm.orchitech.net/api/verifications',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $data,
CURLOPT_HTTPHEADER => $header,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_AUTOREFERER => true,
CURLOPT_COOKIESESSION => true,
CURLOPT_FILETIME => true,
CURLOPT_FRESH_CONNECT => true,
CURLOPT_USERPWD => "testdm:testdm"
];
$ch = curl_init();
curl_setopt_array($ch, $curl_options);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
var_dump($result);
The request header was not sent causing http error code 415 UnsupportedMediaTypeHttpException Hence need to add request header, working fine
I recommend you to use Postman. Just install it and try to make request with it. You'll see the result of your request and than can simply get the request code out.
You can see 'Code' on your top-right corner below 'save', press it and select your language. For your purposes it is PHP->cURL. And the result will look like this:
More about Postman here
I have written a php code that should print the response of a json call. But I am getting NULL output. I have checked my json call is working fine from a restclient.
Here is my code
<?php
$username = "user";
$password = "password";
$postData = array(
'username' => $username,
'password' => $password,
'msisdn' => "111111111"
);
$ch = curl_init('http://ip:<port>/xxx/yyy/zzz');
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json'
),
CURLOPT_POSTFIELDS => json_encode($postData)
));
// Send the request
$response = curl_exec($ch);
// Check for errors
if($response === FALSE){
die(curl_error($ch));
}
// Decode the response
$responseData = json_decode($response, TRUE);
// Print the date from the response
echo $responseData['published'];
var_dump($responseData);
?>
Thanks in advance.
Try:
$responseData = json_decode($response, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new \RuntimeException('Invalid JSON.');
}
okay so i have solved my problem and re-wrote the code. I was actually not providing the correct form of username and password in the json fields which is really a silly mistake.
I am giving my updated code here
`
$data = array("user" => "$username", "pass" => "$password", "msisdn" => "$msisdn");
$data_string = json_encode($data);
$ch = curl_init('http://ip:port/call_center/account/general');
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))
);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
echo $result;
?>`
Thanks everyone.
i want to receive errors from the sms host server
curl_error curl_errno curl_strerror all these function returns error but not in the way i want , mysmshost say that you will receive code like, 202 for wrong mobile number, 301 for wrong authentication, i want to receive those error code please help.
this is my code.
//sending message
$authKey = "my_authentication_key";
$senderId = "sender";
$message = urlencode("Hello Its Working..");
$route = "1";
$campaign = 'Default';
//Prepare you post parameters
$postData = array(
'authkey' => $authKey,
'mobiles' => $number,
'message' => $message,
'sender' => $senderId,
'route' => $route,
'campaign' => $campaign
);
//API URL
$url="http://sms.mysmshost.com/sendhttp.php";
// init the resource
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postData
//,CURLOPT_FOLLOWLOCATION => true
));
//Ignore SSL certificate verification
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
//get response
$output = curl_exec($ch);
//Print error if any
if(curl_errno($ch))
{
echo curl_error($ch)."-curl_error<br/>";
$chcode =curl_errno($ch);
echo $chcode;
echo '<br/>error:' . curl_strerror($chcode)."<br/>";
}
curl_close($ch);
echo $output;
Curl_errorno() is not executing, even if i print them without if condition they give no result.
Try this
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));
$server_output = curl_exec($ch);
curl_close($ch);
print_r($server_output);
So I'm using the only source I've found for sending a post request to Google QPX API. I want to save it in a json_decoded PHP array, but for some reason the $result = curl_exec($ch); line doesn't work, and the json prints onscreen anyways.
Is there something I'm not understanding that is happening in the cURL? Thanks!
$data = array ( "request" => array(
"passengers" => array(
adultCount => 1
),
"slice" => array(
array(
origin => "BOS",
destination => "LAX",
date => "2015-09-09"),
array(
origin => "LAX",
destination => "BOS",
date => "2015-09-10"),
),
solutions => "10"
),
);
$data_string = json_encode($data);
$ch = curl_init('https://www.googleapis.com/qpxExpress/v1/trips/search?key=MY-API-KEY');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch);
curl_close($ch);
This:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
CURLOPT_RETURNTRANSFER - TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
http://php.net/curl_setopt
Set this option to true if you want to save the result in a variable.