I have a PHP problem with my web page. I state that I am developing the site for a FiveM server.
In this page I have to enter the number of players and the number of maximum players that the FiveM server can host.
This is what I wrote:
<?php
$file = file_get_contents('http://fivem.lrfreeroamitalia.it:30120/dynamic.json');
$decode = json_decode($file, true);
$clients = isset($decode['clients']);
$svmaxclients = isset($decode['sv_maxclients']);
echo $decode['clients'] . '/' . $decode['sv_maxclients'];
?>
The problem with this code is that it gives me this error PHP:
Warning: file_get_contents(http://fivem.lrfreeroamitalia.it:30120/dynamic.json): failed to open stream: Connection timed out in /web/htdocs/www.lrfreeroamitalia.it/home/index.php on line 49
Port 30120 of the remote server is open.
P.S. I use Aruba.it as a provider
You use cURL to grab contents and display them.
More Documentation on cURL: https://www.php.net/manual/en/curl.examples.php
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'http://fivem.lrfreeroamitalia.it:30120/dynamic.json',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
));
$response = curl_exec($curl);
curl_close($curl);
$obj = json_decode($response);
echo $obj->clients . '/' . $obj->sv_maxclients;
Related
THE FOLLOWING CODE IS RUNNING SMOOTHLY IN MY LOCALHOST. BUT WHEN I UPLOAD THIS FILE IN WEBSERVER[HOSTINGER], IT IS NOT RUNNING.
$partmsg="https://wapush.in/api/send.php?number=";
$partmsg.=$mobno;
$partmsg.="&type=text&message=Dear Admin User $adminname Your Current Password is ";
$partmsg.=$pwd;
$partmsg.=" - GSFCS&instance_id=63D79C168ACAF&access_token=134ac4a6ec1c01c6825b67ddeba70fa8";
$data = [
'collection' => 'RapidAPI'
];
$curl = curl_init($partmsg);
$response = curl_exec($curl);
echo "<script>alert('Your Password is Send to your Registered Mobile Number.');</script>";
IS THERE ANY OTHER WAY TO EXECUTE THE $URL OTHER THAN RapidAPI that I used.
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://wapush.in/api/send.php?number=91<10_digit_no>&type=text&message=Dear%20Admin%20User%20$adminname%20Your%20Current%20Password%20is&instance_id=<your_instance_id>&access_token=<your_access_token>',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
Try this code. I have tested it. its works with the credentials that you shared. Please don't share your credentials as I see it isfrom gun sheel factory. Remove them immediately
Upvote and accept this as a answer
Im having an issue since I switched from XAMPP to laravel valet (windows variant)
My cURL does work in postman and returns the correct data, but the cURL request in the code now times out no matter what, its requesting on the correct URL.
$url = BaseRequest::getHost();
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url."/locations?location=" . $loc1 . "," . $loc2,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 5,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
));
$response = curl_exec($curl);
curl_close($curl);
$response = json_decode($response, true);
return $response;
This is for a school project where I had to build an API but for time sake its build in the same application as the front end part.
Im using Laravel 7 framework
I have an apache server with centos distribution that I have put a PHP Laravel project on using git. It uses cURL requests to fetch and execute data gotten from endpoints. When I run this on my local virtual machine, it works perfectly. When I run it on my
Here is my code, note the cURL requests:
//NOTE: $this->headers = ["Authorization: Bearer " . myServerAccessToken];
//sets URL and $curl
public function getAdmins() {
$adminsURL = "https://my.test.instructure.com/api/v1/accounts/1/admins?per_page=20";
$curl = curl_init();
self::requestCurl($curl, $adminsURL, $this->headers);
$resp = curl_exec($curl);
//dd($resp); //shows false
return self::setHeaders($curl);
}
//cURL request function: used to set the value of $curl as seen in other functions
public function requestCurl($curl, $url, $headers) {
return curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => TRUE,
CURLINFO_HEADER_OUT => TRUE,
CURLOPT_URL => $url,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_SSL_VERIFYPEER => TRUE,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_FOLLOWLOCATION => TRUE,
CURLOPT_MAXREDIRS => 10,
CURLOPT_HEADER => TRUE
]);
}
//returns "curl errno: 0. Failed to connect to my.test.instructure.com:443; Operation now in progress
//gets the data from a successful curl request, and returns said data
public function setHeaders($curl) {
echo "<h1>SetHeaders()</h1>";
$resp = curl_exec($curl);
$header_size = curl_getinfo( $curl, CURLINFO_HEADER_SIZE );
$header = substr( $resp, 0, $header_size );
$body = substr( $resp, $header_size); //$header_size
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if (curl_getinfo($curl, CURLINFO_HTTP_CODE) != 200) {
echo "<h3>Curl errno: " . curl_getinfo($curl, CURLINFO_HTTP_CODE) . "</h3>";
echo "<h3>Curl error(): " . curl_error($curl) . "</h3>";
dd($body);
return null;
}
$data = json_decode($body, true);
return $data;
}
I can run this code on my local machine and it works perfectly. No complaints, full success. But when I run it on my apache server with a centos distribution, it falls apart. This output is what I get instead:
"SetHeaders()"
"Curl errno: 0"
"Curl error(): Failed connect to my.test.instructure.com:443; Operation now in progress"
"" [dd($body) returns an empty string]
====
I've looked up currl errno: 0 and I've looked up curl error 443, but
The closest I've seen to a problem similar to mine is this page here:
How to resolve cURL Error (7): couldn't connect to host?
But from what I can tell, I already have the suggested solution in my code (CURLOPT_FOLLOWLOCATION => TRUE).
I suspect the problem is with the server itself, but I do not know enough of it to say what. I have looked at the /etc/hosts folder on my server, but I've been told it is configured correctly. With that in mind, what more can I try to solve this problem?
The problem was that my server network had a proxy set. I was able to circumvent it by adding two lines to my curl_setopt_array request:
public function requestCurl($curl, $url, $headers) {
return curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => TRUE,
CURLINFO_HEADER_OUT => TRUE,
CURLOPT_URL => $url,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_SSL_VERIFYPEER => TRUE,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_PROXY => "https://my.proxy.url", //your proxy url
CURLOPT_PROXYPORT => "my_proxy_port_number", // e.g 3333
CURLOPT_FOLLOWLOCATION => TRUE,
CURLOPT_MAXREDIRS => 10,
CURLOPT_HEADER => TRUE
]);
}
We are unable to make the HTTP POST request call to fetch the results and note that autherization type is AWS signature authorization.
In Drupal -8, we have tried with 'AWS connector' module but can not find the exact service for the HTTP POST request with the AWS signature authorization.
Notes:
I got some info on this at drupalize.me site.
<code>
$client = \Drupal::httpClient();
$request = $client->get('https://api.github.com/user', [
'auth' => ['username','password']
]);
$response = $request->getBody();
(Ref: https://drupalize.me/blog/201512/speak-http-drupal-httpclient)
</code>
The auth is for above is basic authentication. But I need for “AWS Signature”
We have tried the below curl code and not able to get the any result. Its showing the white blank screen without any errors.
<code>
$startTimestamp = time();
$amz = gmdate('Ymd\THis\Z', $startTimestamp);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api-runtime.us-east-1.amazonaws.com/endpoint",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\n\t\"campaignArn\":\"arn:aws:test:us-east-1:12456:campaign/test\",\n\t\"numResults\":2,\n\t\"userId\":\"400\"\n}",
CURLOPT_HTTPHEADER => array(
'"authorization: AWS4-HMAC-SHA256 Credential=**********/20191205/us-east-1/Test/aws4_request,
SignedHeaders=content-length;content-type;host;x-amz-date;x-amz-target,
Signature=' . $signature . '"',
'"cache-control: no-cache"',
'"content-type: application/json"',
'"host: test-runtime.us-east-1.amazonaws.com"',
'"x-amz-date:' . $amz . '"',
'"x-amz-target: getrecommendations"'
),
));
$result = curl_exec($curl);
if($result === false) {
echo "Error in cURL : " . curl_error($curl);
}
else {
echo 'no error'.$result;
}
Please check and share the valuable inputs / reference.
I am new to API's so please be easy on me! My school has a ooen data platform API for anyone to use. So, I decided to get a user & key in order to test stuff out. The following code snippet is my script in order to print out the details of a course.
<?php
$APIUSER = “***”; //user value from your application
$APIKEY = “***”; //Api key value from your application
$url = "https://opendata.concordia.ca/API/v1/course/description/filter/000106";
$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 => "GET",
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_HEADER => 0,
CURLOPT_USERPWD => $APIUSER . ":" . $APIKEY
));
$response = curl_exec($curl);
print($response);
?>
I tested my API user & API key on the $url page and it worked. Unfortunately, when I run the script on a local server, there is nothing printing. Am I missing something?