API Newbie - PHP Curl Call - php

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?

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

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'

cURL timing out but postman works (local)

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

CRM show data using PHP CURL

I want to show my data on my CRM using PHP with curl and also using API.
If there is any other method. You can kindly show it to me
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://b.maxmind.ma/api/leads",
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",
CURLOPT_HTTPHEADER => array(
"authtoken: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyfdfdiOjE1ODEwODk2MjJ9.Selc36cmT3XyXH6cGdJ3SN-332kx7"
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
?>
the response show : error code: 1020
I think the problem is that the website you are trying to request is protected by Cloudflare.
When i try by the postman , this is the header response a get :
The error code 1020 you got is from cloudflare .
First, you can try using this solution that bypasses the Cloudflare problem
KyranRana/cloudflare-bypass
If not working, in this case, you must contact the site owner to remove the block (more here)

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