We have properly implemented the Uber Ride Reminder API and we are getting the expected results when requested.
The only issue we are facing is, out of 100+ requests we get the response for any one of those request. Rest of the requests simply responds with empty response.
We are using Following Function to make Curl Post request:
function get_data_post($url, $post_data, $headers = false, $timeout = 3) {
$ch = curl_init();
$timeout = $timeout;
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
if($headers)
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
And here are the input parameters:
URL: https://api.uber.com/v1/reminders
Request Object:
{"reminder_time":1469880000,"phone_number":"+911231231234","event":{"time":1469894400,"name":"DIY Artistic Frames","location":"pH Designs","latitude":"23.03743","longitude":"72.5682099"}}
Headers:
Cache-Control: no-cache
Accept: application/json
Content-Type: application/json
Authorization: Token xyxxyxxyx-Aux
Related
I have a Lambda function on AWS for which I have created a trigger with Security. I set the url ($url) to be the API endpoint listed in the "triggers" section of the lambda function, and the x-api-key (instead of my_api_key below) to be the one listed in the trigger "API key".
However, if I try to make a simple call to the lambda function like so:
$url = "https://abcd1234.execute-api.us-east-2.amazonaws.com/default/lambda_function_name";
$header = array(
"Content-Type" => "application/json",
"Accept" => "application/json",
"x-api-key" => "my_api_key",
);
$data = array(
"email_address" => $_POST['email'],
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, json_encode($header));
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
$result = curl_exec($curl);
curl_close($curl);
I obtain the response "Forbidden".
What is the issue here?
Is there some AWS configuration I need to change?
Or is the syntax or place to add the x-api-key wrong?
Thanks in advance!
Maithreya
I am assuming you are connecting to AWS API Gateway. Your endpoint URL must include the stage and resource, i.e.:
https://abcd1234.execute-api.us-west-2.amazonaws.com/prod/send_email_alert
Calling the API gateway base endpoint URL will result in this error.
I have to call rest api function in codeigniter project . Data is returning from the api call using Postman . In Postman first i generate a token and put that generated token in the rest call. I have to do the same process in codeigniter. First i have to generate token and pass that token in second api call to get the desired result.
My Controller code :
public function getToken()
{
$url='http://localhost:8080/myapplication/rest/api/validate-request';
$key=$this->get_data($url);
}
public function get_data($url)
{
//create a new cURL resource
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
//curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, R_U_NAME.':'.R_U_PWD);
$result = curl_exec($ch);
$result = json_decode($result);
//var_dump($result);
//close cURL resource
curl_close($ch);
}
Here var_dump($result) is returning null value.
I donot had to pass username and password for authentication .Instead i had to post postman username and password in my function. Now I am getting the token back from the postman.
private function get_data($url)
{
$member_data = array(
'username' => R_U_NAME,
'password' => R_U_PWD
);
//create a new cURL resource
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $member_data);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
I want to get my form data back from kobo api (https://kc.kobotoolbox.org/api/v1/). I registered client application from kobo. To authorize client application, i run below url but it results in
http://localhost/kobo/o/authorize?client_id=MY_CLIENT_iD&response_type=code&state=xyz
Object not found!
The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.
If you think this is a server error, please contact the webmaster.
Error 404
localhost
Apache/2.4.27 (Win32) OpenSSL/1.0.2l PHP/7.1.9
Similarly request for access token also result in same error
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost/o/token/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=authorization_code&\ncode=PSwrMilnJESZVFfFsyEmEukNv0sGZ8&\nclient_id=MY_CLIENT_ID&\nredirect_uri=http://localhost:30000");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, "USERNAME" . ":" . "PASSWORD");
$headers = array();
$headers[] = "Content-Type: application/x-www-form-urlencoded";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
echo $result;
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
Object not found!
The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.
If you think this is a server error, please contact the webmaster.
Error 404
localhost
Apache/2.4.27 (Win32) OpenSSL/1.0.2l PHP/7.1.9
I am using xampp and curl is enabled.
You have to use https://kc.kobotoolbox.org/api/v1/ not https://kf.kobotoolbox.org/api/v1/
checkout docs here http://help.kobotoolbox.org/managing-your-project-s-data/rest-services
MY WORKING CODE:
$url = 'https://kc.kobotoolbox.org/api/v1/stats';
$data = array (
// 'q' => 'nokia'
);
$params = '';
foreach($data as $key=>$value)
$params .= $key.'='.$value.'&';
$params = trim($params, '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url.'?'.$params ); //Url together with parameters
//curl_setopt($ch, CURLOPT_CONNECTTIMEOUT , 7); //Timeout after 7 seconds
//curl_setopt($ch, CURLOPT_HEADER , false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$header = array();
$header[] = 'Content-length: 0';
$header[] = 'Content-type: application/json';
$header[] = 'Authorization: Token xxxxxxxx';
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
//curl_setopt($ch, CURLOPT_USERPWD, "USERNAME" . ":" . "PASSWORD");
$result = curl_exec($ch);
if(curl_errno($ch)) //catch if curl error exists and show it
echo 'Curl error: ' . curl_error($ch);
echo $result;
I am trying to use Guzzle instead of directly using cURL to achieve and HTTP request. How do I make this same type of request but with Guzzle? Or should I just stick to cURL?
$ch = curl_init();
// Set the URL
curl_setopt($ch, CURLOPT_URL, $url);
// don't verify SSL certificate
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
// Return the contents of the response as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Follow redirects
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
// Set up authentication
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$token:X");
I keep running into 401 Unauthorized error. I know I have correct credentials. What makes me think I am not on the right track is the Guzzle docs stating: auth is currently only supported when using the cURL handler, but creating a replacement that can be used with any HTTP handler is planned. But from my understanding Guzzle defaults with cURL.
$guzzleData = [
'auth' => [$token, 'X'],
'allow_redirects' => true,
'verify' => false,
];
$client = new \Guzzle\Http\Client();
$request = $client->get($url, $guzzleData);
$response = $request->send();
Here is the solution:
$client = new \Guzzle\Http\Client();
$request = $client->get($url);
$request->getCurlOptions()->set(CURLOPT_SSL_VERIFYHOST, false);
$request->getCurlOptions()->set(CURLOPT_SSL_VERIFYPEER, false);
$request->getCurlOptions()->set(CURLOPT_RETURNTRANSFER, true);
$request->getCurlOptions()->set(CURLOPT_FOLLOWLOCATION, true);
$request->getCurlOptions()->set(CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$request->getCurlOptions()->set(CURLOPT_USERPWD, "$token:X");
$response = $request->send();
The solution I was able to get working for Guzzle6 is:
$headers = array();
$headers['grant_type'] = 'client_credentials';
$headers['client_id'] = $clientid;
$headers['client_secret'] = $clientSecret;
$response = $this->client->post($urlAuth, ['form_params' => $headers]);
$output = $response->getBody()->getContents();
ie the header array has to be wrapped in 'form_params'
This is my code, the credentials are correct, why do I get Authentication Failed? It is probably the headers but I can't figure out which.
<?php
$ch = curl_init();
$headers = array("X-PAYPAL-SECURITY-USERID" => "--",
"X-PAYPAL-SECURITY-PASSWORD" => "--,
"X-PAYPAL-SECURITY-SIGNATURE" => "--",
"X-PAYPAL-APPLICATION-ID" => "APP-80W284485P519543T",
"X-PAYPAL-REQUEST-DATA-FORMAT" => "NVP",
"X-PAYPAL-RESPONSE-DATA-FORMAT" => "JSON",
"X-PAYPAL-SANDBOX-EMAIL-ADDRESS" => "--",
"Accept" => "application/json",
'Content-Type: application/x-www-form-urlencoded');
curl_setopt($ch, CURLOPT_URL, "https://svcs.sandbox.paypal.com/AdaptivePayments/ConvertCurrency");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "requestEnvelope.errorLanguage=en_US&
baseAmountList.currency(0).code=USD&
baseAmountList.currency(0).amount=1&
baseAmountList.currency(1).code=EUR&
baseAmountList.currency(1).amount=1&
baseAmountList.currency(2).code=SEK&
baseAmountList.currency(2).amount=1&
convertToCurrencyList.currencyCode=USD");
$result = curl_exec($ch);
if(empty($result))die("Error: No response.");
else
{
$json = json_decode($result);
echo $result;
}
curl_close($ch);
?>
In return I get this:
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
X-EBAY-SOA-REQUEST-ID: 144ee8af-72d0-a486-d657-e4f2fffe5f49!AdaptivePayments!10.72.109.101![]
X-PAYPAL-SERVICE-VERSION: 1.0.0
X-PAYPAL-SERVICE-NAME: {http://svcs.paypal.com/types/ap}AdaptivePayments
X-EBAY-SOA-RESPONSE-DATA-FORMAT: XML
X-PAYPAL-OPERATION-NAME: ConvertCurrency
Cache-Control: no-cache
X-PAYPAL-ERROR-RESPONSE: TRUE
X-EBAY-SOA-MESSAGE-PROTOCOL: NONE
Content-Type: text/xml;charset=UTF-8
DC: origin1-svcs.sandbox.paypal.com
Date: Sun, 23 Mar 2014 10:44:48 GMT
Transfer-Encoding: chunked
Connection: keep-alive
Connection: Transfer-Encoding
Set-Cookie: Apache=10.72.109.11.1395571488549095; path=/; expires=Tue, 15-Mar-44 10:44:48 GMT
Set-Cookie: DC=origin1-svcs.sandbox.paypal.com; secure
<?xml version='1.0' encoding='UTF-8'?><ns3:FaultMessage xmlns:ns3="http://svcs.paypal.com/types/common" xmlns:ns2="http://svcs.paypal.com/types/ap"><responseEnvelope><timestamp>2014-03-23T03:44:48.571-07:00</timestamp><ack>Failure</ack><correlationId>95445f07b579b</correlationId><build>10175386</build></responseEnvelope><error><errorId>520003</errorId><domain>PLATFORM</domain><subdomain>Application</subdomain><severity>Error</severity><category>Application</category><message>Authentication failed. API credentials are incorrect.</message></error></ns3:FaultMessage>
You're using the sandbox shared X-PAYPAL-APPLICATION-ID which is correct for sandbox, but will not work in live. There you'd need your own application's ID.
My example also includes the header "X-PAYPAL-SERVICE-VERSION": "1.0.0".