I have a very simple API request that sends a JSON encoded string with a userID and password. When I try to execute the command through my Chrome Extension - Advanced Request Client or through Swagger it works perfect, but the exact same request in CURL is failing, the response is returning invalid password.
The code should be really straight forward, is there some setting in CURL I'm missing?
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost:8080/api");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"userid\":\"1231\",\"password\":\"123\"}");
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = "Content-Type: application/json";
$headers[] = "Accept: application/json";
$headers[] = "X-Databasename: x";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
Related
I'm relatively new to cURL in PHP, and I'm looking to cURL against an API where where the API Key is in the header. Here's what I have thus far:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://publicapi.ohgo.com/api/v1/reported-conditions?region=ne-ohio');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
$headers = array();
$headers[] = 'Accept: application/json';
$headers[] = 'Authorization: APIKEY 016c348e-58fe-486f-ad72-9b2f41c55a56';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
Unfortunately, after running this script, I get a 403 error printed to the screen after a few seconds:
Error:Failed connect to publicapi.ohgo.com:443; Connection timed out
How can I get these API results? Worth noting:
1) I have changed a few characters from my actual API key to make the one above
2) API documentation is here: https://publicapi.ohgo.com/docs/v1/swagger/index#/Reported32Conditions
It's failed to connect to API server. Try to increase connect timeout for that.
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); // value in seconds
Try to skip ssl verification this will solve your issue
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://publicapi.ohgo.com/api/v1/reported-conditions?region=ne-ohio');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // Skip SSL Verification
$headers = array();
$headers[] = 'Accept: application/json';
$headers[] = 'Authorization: APIKEY 016c348e-58fe-486f-ad72-9b2f41c55a56';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
I have a cURL GET statement:
curl -v -X GET "https://admiraltyapi.azure-api.net/uktidalapi/api/V1/Stations/{stationId}" -H "Ocp-Apim-Subscription-Key: {subscription key}" --data-ascii "{body}"
that works fine in a terminal window (with my sub key) - I now want to use it in a PHP script to retrieve the associated JSON. Code suggestions for achieving this appreciated...
Not tested but you need something like this with PHP CURL.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://admiraltyapi.azure-api.net/uktidalapi/api/V1/Stations/{stationId}');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_TRANSFERTEXT, true);
$headers = array();
$headers[] = 'Ocp-Apim-Subscription-Key: {subscription key}';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
I would like to send a form data to API this API has a key and profile must be provided then after sending data by post request i have to send get request to get the info back
this is the post function they provide :
curl -k -X POST --header "X-API-KEY:TOKEN" -F "profile=B2B" -F
"data_file=#/var/tmp/testfile.csv" -F "md5=ae0eca4f5671cbdc19340a1df2567611"
https://api.cloudHygiene.com/task/run
and this is the get function they provide :
curl -k -X GET --header "X-API-KEY:TOKEN"
https://api.cloudhygiene.com/task/get_file?
package_name=hWviW2hLui_1477652942&f
ilename=_report.xls
Are you asking how to run the cURL command in PHP? If so here is the first part of the cURL request in PHP:
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.cloudHygiene.com/task/run");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = "X-Api-Key: TOKEN";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
Here is the second part:
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.cloudhygiene.com/task/get_file?");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
$headers = array();
$headers[] = "X-Api-Key: TOKEN";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
Again, I am only speculating you are asking to run the cURL request in PHP. If not, I suggest rewording your question and providing more details.
I'm trying to send a request to the RingCentral API to trigger an SMS message to be sent. I've read the documentation and it appears as though I'm posting all of the data in the correct format but I'm getting an error of "Unsupported Media Type".
Does anyone see anything wrong with my code, or is do any of you guys have experience with this API?
$data = array("from" => "+10000000000", "to" => "+100000000", "text" => "test_sms_message");
$data_string = json_encode($data);
$ch = curl_init('https://platform.devtest.ringcentral.com/restapi/v1.0/account/~/extension/~/sms');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = array();
$headers[] = "Authorization: Bearer ".$auth_token;
$headers[] = "Accept: application/json";
$headers[] = "Content-Type: application/x-www-form-urlencoded";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
print_r($result);
So I'm going to post an answer to my own question and include the entire code I used. This code will allow you to get an authorization token first and then use that token to send a request to the RingCentral REST API. I couldn't find a working PHP example anywhere online so I'm sure this will help someone else out there.
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://platform.devtest.ringcentral.com/restapi/oauth/token");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "username=+1XXXXXXXXX&password=XXXXXXXXX&extension=XXX&grant_type=password");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, "XXXXXXXXXXXX" . ":" . "XXXXXXXXXXXXXXXXX");
$headers = array();
$headers[] = "Accept: application/json";
$headers[] = "Content-Type: application/x-www-form-urlencoded";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
$decoded_results = json_decode($result, true);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
echo '<pre>';
print_r($result);
curl_close ($ch);
$auth_token = $decoded_results['access_token'];
// LINE BREAK
$data_string = '{"to": [{"phoneNumber": "+INSERTNUMBER"}],"from": {"phoneNumber": "+INSERTNUMBER}"},"text": "Test SMS message from Platform server"}';
$ch = curl_init('https://platform.devtest.ringcentral.com/restapi/v1.0/account/~/extension/~/sms');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = array();
$headers[] = "Authorization: Bearer ".$auth_token;
$headers[] = "Accept: application/json";
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
print_r($result);
Is it possible to convert this curl command to PHP?
curl -H "cwauth-token: CwEqP_oQXCiDKt3ph0leZ2AlKsqE6E4N " "https://reports.voluum.com/report?from=2014-09-24T00%3A00%3A00Z&to=2014-0925T00%3A00%3A00Z&tz=Europe%2FWarsaw&sort=visits&direction=desc&columns=campa ignName&columns=visits&columns=clicks&columns=conversions&columns=revenue&co lumns=cost&columns=profit&columns=cpv&columns=ctr&columns=cr&columns=cv&colu mns=roi&columns=epv&columns=epc&columns=ap&columns=errors&groupBy=campaign&o ffset=0&limit=100&include=active"
Here's the fish:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://reports.voluum.com/report?from=2014-09-24T00%3A00%3A00Z&to=2014-0925T00%3A00%3A00Z&tz=Europe%2FWarsaw&sort=visits&direction=desc&columns=campa ignName&columns=visits&columns=clicks&columns=conversions&columns=revenue&co lumns=cost&columns=profit&columns=cpv&columns=ctr&columns=cr&columns=cv&colu mns=roi&columns=epv&columns=epc&columns=ap&columns=errors&groupBy=campaign&o ffset=0&limit=100&include=active");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
$headers = array();
$headers[] = "Cwauth-Token: CwEqP_oQXCiDKt3ph0leZ2AlKsqE6E4N";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
Here's how to fish:
https://curl.haxx.se/docs/manpage.html
https://secure.php.net/manual/en/function.curl-setopt.php
And here's a few fishing machines you can use:
http://curl.trillworks.com/#php
https://incarnate.github.io/curl-to-php/
And BTW, here's why you should consider fishing alternatives:
Requests for PHP
Guzzle, PHP HTTP Client