I have a cURL request in my code which works fine when running locally:
$url = "http://ipinfo.io/{$_SERVER['REMOTE_ADDR']}";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$response = curl_exec($ch);
$locale = json_decode($response);
and returns a JSON as expected. Our production system is on Google App Engine, however, where I get the website version for a browser rather than the JSON.
I can get this cURL request to work if I change
google_app_engine.enable_curl_lite = "1"
in the php.ini in the root directory of my project to
extension = "curl.so"
but Google's documentation insists the former is to be used on production. Additionally, using the latter breaks things like Monolog's SlackHandler.
Is there a way to get the JSON from this cURL request while still using Google's "cURL Lite"?
From the ipinfo.io documentation:
"We do a little bit of magic on the server to determine if we should send the JSON response or the webpage. We usually get it right, but if you're seeing the webpage instead of the JSON (or want to check the JSON output in a browser) you can force the JSON response by adding /json to the end of the URL"
Adding /json to the end of the URL worked for me in this case, but I wanted a more general solution. Since Google's cURL Lite uses their URL Fetch in the background, ipinfo.io's "magic" is somehow getting confused. I found that by specifying the Accept header then the /json addition wasn't required. In PHP:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
Thanks to the folks on the #php channel of NashDev Slack for helping me on this one!
Related
Thank you for your time.
My purpose for posting this question was to see how I can approach the problem of parsing the authorization code I get from the redirect to use that code in a subsequent function to get the oauth bearer token.
What libraries can I use similar to python's request module and web module to achieve the same goal as the following code?
class ILToken(object):
def GET(self):
form = web.input(code=None, scope=None)
As an outcome, here is the following example. I have a redirect url set to "http://redirect_example.com/oauth/". When I click a button, the instant login link ("https://api.provider.com/oauth/authorize?response_type=code&redirect_uri=http://redirect_example.com/oauth/...") redirects me to "http://redirect_example.com/oauth/?code=ExampleAuthCode".
I've tried making use of the curl library in php with no positive progress using code similar to the following.
Code
# Initialize curl request parameters
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, env('INSTANT_LOGIN_URI'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, TRUE); // We'll parse redirect url from header.
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
# Set variables and execute
$output = curl_exec($ch);
$response_code = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
$redirect = curl_getinfo($ch, CURLINFO_REDIRECT_URL);
curl_close($ch);
# Print Results
print $redirect;
Related articles say to set FOLLOWLOCATION to true and use the CURLINFO_EFFEFCTIVE_URL variable to return the code. This did not work for me. I am using php v7.4 on an ubuntu machine. The php app is using the laravel framework and loading behind an nginx v1.21 web server.
How can I get the destination URL using cURL?
PHP CURL redirect
I've been going round in circles trying to get this bit of code working. The problem I am facing is that there could be any number of places where something is wrong and I'm not experienced enough with cURL and API requests to know if I've just done something simple and silly somewhere. The code below is supposed to fetch a JSON response. What I am currently getting is "false". The API developer keeps giving me a CLI sample and I don't know how to "translate" that into something I can use in PHP.
I have to hide the domain, service name and authentication details in my examples.
The string I was given:
'https://[domain]/agw/latest/services/[service]-api/latest/api/v2/[service]-actual-prizes -vk -H "Proxy-Authorization: Basic [authstr]"'
([authstr] is the username and password, separated by a colon and BASE64 encoded - the API dev has confirmed that my authorisation string is correct)
What I have been trying:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://[domain]/agw/latest/services/lottery-api/latest/api/v2/sportka-actual-prizes');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Proxy-Authorization: Basic '.$authstr.'"
,"Content-type: application/json"
));
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
If I understand this correctly (and I'm not sure that I do), then I'm passing the URL (without flags), saying that I don't want a header in the response (I've tried TRUE as well without any success) and then passing headers with my request that includes the authorisation.
I've tried file_get_contents with a stream_context_create header but that fails too.
Am I missing a header option or flag or something in my cURL code?
New Twilio developer here. My app uses the IBM Watson Speech-to-text Add-on, but I'm having trouble accessing the results payload in my callback. I can't find helpful documentation or any discussion of the issue in forums.
What I know/What I've tried
The payload resource exists – I'm able to access it directly via browser.
Using the syntax prescribed by the Twilio PHP helper library client returns a 61005 "Bad request" error:
$request = $client->v1->lookups
->phoneNumbers("+1XXXXXXXXXX")
->fetch(
array(
"AddOns" => "ibm_watson_speechtotext",
));
Using cURL to get the resource directly has been equally unfruitful, returning an empty string.
$request = json_decode($_REQUEST['AddOns']);
error_log("URL: ".$request->results->ibm_watson_speechtotext->payload[0]->url);
$ch = curl_init($request->results->ibm_watson_speechtotext->payload[0]->url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$account_sid:$token");
$json = curl_exec($ch);
if($json === false) {
error_log("cURL error: ".curl_error($ch));
error_log(print_r($json,true));
}
curl_close($ch);
$obj = json_decode($json);
Any recommendations?
The following resources should help you find the results you're looking for.
Your first code snippet above doesn't apply (Lookup is a different product).
instead you will want to use the add-on results api to grab the results.
https://www.twilio.com/docs/api/add-ons/results-api
For your second snippet, you will need to enable follow redirect option with CURL.
Clients will need to follow the redirect to receive the data
associated with a Payload resource.
These may also help as you explore add-ons:
https://www.twilio.com/docs/api/add-ons/using-add-ons#add-on-results-available-callback
and
https://www.twilio.com/docs/guides/voice/how-to-use-recordings-add-ons-in-python
I have Interspire(email marketing) installed on my server. API calls can be sent to a php file that handles xml requests. I am using curl to send the request and it looks like this
$ch = curl_init($this->api_endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
$result = #curl_exec($ch);
return $result;
$this->api_endpoint is a url that points to the php file that handles the xml request.
I am trying to make the whole thing work faster and I thought if I didn't have to sent the request out to the internet and back maybe I could save some time, but I am not sure if it is possible to send the request "locally"... I'd like to possibly have $this->api_endpoint equivalent to ../../xml.php I have actually tried this, but it doesn't work. Can an API call be handled within a server and not need to be sent out to the internet to work?
I am trying to use the Qualys API v2 to get an xml host list returned. I think you must use cURL, but I am unfamiliar with it. Here is my code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_URL, "https://qualysapi.qualys.com/api/2.0/fo/asset/host/?action=list&details=Basic");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-Requested-With: Manitowoc Service Account'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$xml = curl_exec($ch);
curl_close();
$hostobj = simplexml_load_string($xml);
Actually, curl is not required for QualysGuard API calls. As long as you can make https calls you can use any method you would like. But curl is a nice framework because everything is already implemented (with perl, another alternative would be LWP).
I have not been able to find the issue with your code, but I posted a perl script that launches and downloads a Qualys report by making API requests "using WWW::Curl::Easy": https://community.qualys.com/docs/DOC-3222
I hope it could help you to write your own perl API request with libcurl.
I see nothing wrong with your code, but of course we can't see what values you are using for the username and password, and whether those credentials actually exist in QualysGuard.
To get status/error information from curl for a given request, use curl_getinfo(), curl_error(), and curl_errno() as described in the cURL Manual.