Error Timeout 504 GET Request JSON cURL PHP with HTTPheader - php

I have a PHP script to request JSON data with cURL, PHP and HTTPheader, before it worked but suddenly it is not working, and I got Gateway Timeout 504.
This PHP script run from Linux VPS Hosting, with specs:
OS Debian 11,
Apache/2.4.54 (Debian)
mod_fcgid/2.3.9
OpenSSL/1.1.1n
PHP Version 7.4.30.
curl 7.74.0
And this is my code:
<?php
$consid = "my_id";
$secretKey = "my_secretkey";
date_default_timezone_set('UTC');
$tStamp = strval(time()-strtotime('1970-01-01 00:00:00'));
$signature = hash_hmac('sha256', $consid."&".$tStamp, $secretKey, true);
$encodedSignature = base64_encode($signature);
$ch = curl_init();
$headers = array(
'X-cons-id: '.$consid.'',
'X-timestamp: '.$tStamp.'',
'X-signature: '.$encodedSignature.'',
'Content-Type: Application/JSON',
'Accept: Application/JSON'
);
curl_setopt($ch, CURLOPT_URL, "https://domain.tld/aplicaresws/rest/bed/ref/kelas");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_CIPHER_LIST, 'DEFAULT#SECLEVEL=1');
$content = curl_exec($ch);
$err = curl_error($ch);
$data = json_decode($content,true);
foreach ($data["response"] as $record) {}
$data2 = array( 'data' => $record );
$data3 = json_encode($data2);
$fp = fopen("bedlist.json", "w");
fwrite($fp, $data3);
fclose($fp);
curl_close($ch);
?>
Please help review my script what's wrong with this curl PHP code?

Related

Sending an voice file to API (laravel) - 5.4

I am trying to send a voice file to my api built in laravel. Everything seems right in my code but when i send the file to my api, i check at my api side if the data posted has file contained in it but i keep getting false. Am i posting my voice file wrongly.
Path: Absolute path
PHP version : 7.0
$endPoint = 'https://api.domain.com/api';
$apiKey = '**********';
$url = $endPoint . '?key=' . $apiKey;
$curlFile = new \CurlFile('/Users/public/Voice/aaaah.wav');
$data = [
'message' => 'First Voice',
'file' => $curlFile,
];
$ch = curl_init();
$headers = array();
$headers[] = "Content-Type: multipart/form-data";
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($data));
$result = curl_exec($ch);
$result = json_decode($result, TRUE);
curl_close($ch);
return $result;

PHP cURL from localhost getting Missing Parameters error

I am trying to send a cURL post request from my localhost, but I am getting Missing parameters error.When I try from REST client mozilla addon it is working fine. Below is my php code .
$file = tempnam(sys_get_temp_dir(), 'AMZREO');
$fh = #fopen($file, 'w+');
foreach($input as $curl_response)
{
fputcsv($fh, $curl_response, ',', '"');
}
// ... close the "file"...
#fclose($fh);
$post = array(
'file'=>'#'.$file
);
print_r($post);
$header[] = 'Authorization: Token '.$token;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url.'?target='.$target);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
curl_setopt($ch, CURLOPT_HTTP200ALIASES, array(400, 500));
$response = curl_exec($ch);
Below is the response
print_r($response);
{"status": "Failure", "message": "Missing Parameters"}
And I am able to see created file in my temp folder. And when I print_r the $post array I am getting this result.
Array
(
[file] => #C:\Users\acer\AppData\Local\Temp\AMZEE93.tmp
)
Please help me in this regard.Any help would be greatly appreciated.

Wowza Cloud API: curl requests

I can't make it work.. :( I have this function (for create passthrough transcoder), when I run I see NULL in the web. If I test directly from the browser with the url, it does notify me that there is a problem an auth (apikey and acceskey)
function createPassthrough($name, $source_url, $recording = null)
{
$url = "https://sandbox.cloud.wowza.com/api/v1/transcoders";
$json = '{
"transcoder":{
"billing_mode":"pay_as_you_go",
"broadcast_location":"eu_belgium",
"delivery_method":"pull",
"name":"prueba",
"protocol":"rtsp",
"source_url":"url_camara",
"transcoder_Type":"passthrough",
"low_latency":true,
"buffer_size":0,
"play_maximum_connections":100,
"stream_smoother":false
}
}';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept:application/json; charset=utf-8',
'Content-Type: application/json; charset=utf-8',
'wsc-api-key:' . $apiKey,
'wsc-access-key:' . $accessKey,
));
$result = curl_exec($ch);
curl_close($ch);
$obj = json_decode($result);
var_dump($obj);
}
What am I doing wrong? Thanks in advance.
You should check for curl errors after $result = curl_exec($ch);.
// Check for errors and display the error message
if($errno = curl_errno($ch)) {
$error_message = curl_strerror($errno);
echo "cURL error ({$errno}):\n {$error_message}";
}

JSON RPC 2.0 API Call using CURL in PHP - Changelly API

I am trying to call changelly API with below codes but it is returning "Unauthorized" in response.
Appreciate if someone can help in identifying the mistake I am making in below code.
$API_URL = 'https://api.changelly.com';
$API_KEY = 'XXXXX';
$API_SECRET = 'XXXXX';
$message = array();
$message['jsonrpc'] = '2.0';
$message['method'] = 'getMinAmount';
$message['params'] = array('from' => 'BTC', 'to' => 'LTC');
$message['id'] ='12345';
$data = json_encode($message);
$sign = hash_hmac('SHA512', $data, $API_SECRET);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $API_URL);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_GET, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/json",'Authorization:api-key: '.$API_KEY.':sign: '.$sign));
$final_result = curl_exec($ch);
curl_close($ch);
echo '<pre>';
print_r($final_result);
Changelly API guide is at https://changelly.com/developers
Thanks
in your code you have wrong set of headers.
please check this example: https://github.com/changelly/changelly-examples/blob/master/php/example.php, hope it helps.

web API POST curl returns 'not found anything matching the request URI'

i try to access a web api with json encoded and $POST method in php. After a few tries, i get this message:
Not Found - The server has not found anything matching the request URI
.You can get technical details here. Please continue your visit at our
home page.
Why? thank you, my code is:
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
//DADOS DE ACESSO
$id = "xxx";
$retailerName = "xxx";
$key = "xxx";
//PRODUTO
//vars of product:fictional data
$upc = "799366289999";
$amount = "20.00";
//URL DE ACESSO
$function = "requestActivateCode";
//usado pra criar a canonical
$resPath = "/v1/activateCode";
$url = "https://111.222.333.444/v1/";
$urlCall = $url.$function;
//HTTP HEADERS
$date = gmdate("M, d Y H:i:s \G\M\T", time());
$contentType = "Content-Type: application/json; charset=UTF-8";
$accept = "Accept: application/json";
$xIncommDateTime = substr(substr_replace(date('c'), substr(round(microtime(), 3), 1, 8), 19, 0), 0, -6) . "Z";
//authorization
$type = "application/json; charset=UTF-8";
$canonical = ($xIncommDateTime) + ($type) + ($resPath);
$signature = hash_hmac ('sha1', $key, $canonical);
$idEncoded = base64_encode($id);
$signatureEncoded = base64_encode($signature);
$authorization = "Incomm ". $idEncoded .':'. $signatureEncoded;
$header = array("Date: ".$date, $contentType, $accept, "X-Incomm-DateTime: ".$xIncommDateTime, "Authorization: ".$authorization);
//PARAMS example
$params = array("RetailTransactionRequest" =>
array(
"code" => "0000002381237220",
"amount" => $amount,
"upc" => $upc,
"transactionID" => "1234",
"dateTime" => $xIncommDateTime,
"retailerName" => $retailerName
)
);
$request = json_encode($params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $urlCall);
//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
//curl_setopt($ch, CURLOPT_SSLVERSION, 1);
//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
curl_setopt($ch, CURLOPT_PORT, 443);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
echo curl_setopt($ch, CURLINFO_HEADER_OUT, true); // enable tracking
$result = curl_exec($ch);
echo $result;
$headerSent = curl_getinfo($ch, CURLINFO_HEADER_OUT ); // request headers
echo "<pre>";
print_r($headerSent);
echo "</pre>";
if($result === false)
{
echo 'Curl error: ' . curl_error($ch);
}
else
{
echo 'Operation completed without any errors';
echo "<pre>";
print_r($result);
echo "</pre>";
}
In netstat -l ip my port 443 is listening, but the host in ping dont. I want to know how to solve the problem, thank you
If the code is exactly what is shown I don't think that your server's ip address is correct. Instead of using the IP address (111.222.333.444) used in the example, substitute the IP address of your site or the domain name of your site.
$url = "https://111.222.333.444/v1/"

Categories