cURL timing out but postman works (local) - php

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

Related

How to Execute an API in browser using PhP

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

Problem to get Data with CURL from an API "*.glitch.me" Domain

I am trying to get the JSON data from a Website called
"https://thesimpsonsquoteapi.glitch.me/"
It has some sort of protection and take a while to open.
The Problem. After the site is loaded, you get access to some API calls like
"https://thesimpsonsquoteapi.glitch.me/quotes"
If I try it in Postman, I get back the right JSON response also if I open it up in the browser.
But if I try the to load the Data via curl in PHP i get the html code back from the first protection side. What can I do to get the JSON like in Postman.
$curl = curl_init();
$url = 'https://thesimpsonsquoteapi.glitch.me/quotes?count=1';
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
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);
thx for any help

WordPress JSON API AUTH with PHP Curl

I am working with wordpress and using this plugin https://wordpress.org/plugins/json-api-auth/ for JSON API Auth and I am trying to return success with curl in PHP but whenever I run the code I am getting {"status":"error","error":"Invalid username and\/or password."}
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://example.com/api/auth/generate_auth_cookie/?username="abcd"&password="abcd"',
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;
?>
But when I put this in the browser url https://example.com/api/auth/generate_auth_cookie/?username=abcd&password=abcd I am getting success
{"status":"ok","cookie":"xyz","cookie_name":"xyzxyz","user":{"id":1,"username":"abcd","nicename":"abcd","email":"abcd#abcd.com","url":"","registered":"2020-09-10 10:42:41","displayname":"abcd","firstname":"abcd","lastname":"abcd","nickname":"abcd","description":"","capabilities":{"administrator":true},"avatar":null}}
Can you help me what I am doing wrong with curl in PHP?
Original version of CURLOPT_URL that I have used in code
CURLOPT_URL => 'https://example.com/api/auth/generate_auth_cookie/?username="'.$this->input->post('email').'"&password="'.$this->input->post('password').'"',
Remove the " from the cURL url query string. The WordPress PHP handling your request would read your username as "abcd" instead of abcd as you are trying to achieve.
CURLOPT_URL => 'https://example.com/api/auth/generate_auth_cookie/?username="abcd"&password="abcd"'
Should be
CURLOPT_URL => 'https://example.com/api/auth/generate_auth_cookie/?username=abcd&password=abcd'

API Newbie - PHP Curl Call

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?

AtTask API 'PUT' CURL Fails to Update

I have been working on some code to automate an update to a specific task in AtTask using the API. I am finding some problems using PHP's CURL request for a PUT command. Here is a copy of the code:
$updateURL = "https://[COMPANYURL].attask-ondemand.com/attask/api/v4.0/task?updates={'ID':'TASKID','name':'Ok. Here we go again','commitDate':'2015-10-27T17:30'}";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $updateURL,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_HTTPHEADER => array(
"sessionid: " . $this->sessionId
),
));
$attask_update_json = curl_exec($curl);
The code executes with out any errors being reported, but the return response is blank. I have verified my URL using postman and it processes correctly, but I can not get the code to process in PHP. Any help would be appreciated.

Categories