I need to get all surveys through api call. The api provider give me this
Example curl Authorization:
curl –header “Content-Type: application/json” --header “Authorization: Basic
“API_KEY””-X GET “url”
I have set curl code like this
$access = 'apikey';
$headers = array(
'Content-Type: application/json',
'Authorization: Basic'.$access
);
$url = 'url';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER ,0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST ,0);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
curl_close($ch);
print_r($result);
But not getting any data. Can anyone please advise me what am i doing wrong?
Related
I am trying to create a cURL api, let's say the below is my curl request code, where http://localhost/api/endpoint.php is my endpoint. What would actually be going on, code-wise in the endpoint.php file? How do I actually get the data that is sent via the cURL request, like the CURLOPT_USERPWD data, in the endpoint.php page? Thanks.
$ch = curl_init();
$url = 'http://localhost/api/endpoint.php';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_USERPWD, 'alex:password123');
$headers = array(
'Accept: application/json',
'Content-Type: application/x-www-form-urlencoded',
'Accept-Language: en_US'
);
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$vars['grant_type'] = 'client_credentials';
$req = http_build_query($vars);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
$response = curl_exec($ch);
You can access to these data via the $_SERVER superglobal.
Endpoint :
<?php
$user = $_SERVER['PHP_AUTH_USER']; // alex
$password = $_SERVER['PHP_AUTH_PW']; // password123
in your file endpoint.php you can print your posted value using
print_r($_POST);
after your curl print response like
print_r($response);
I am writing code to send notifications to the Apple push notification servers (APNs) using PHP Laravel. It says in the documents that it requires HTTP/ HPACK header compression.
I've tried using cURL
$cURLConnection = curl_init();
if(strcmp(env('APP_ENV'), 'production') == 0) {
curl_setopt($cURLConnection, CURLOPT_URL, 'api.push.apple.com:443');
} else {
curl_setopt($cURLConnection, CURLOPT_URL, 'api.development.push.apple.com:443');
}
curl_setopt_array($cURLConnection, [
CURLOPT_RETURNTRANSFER =>true,
CURLOPT_HTTP_VERSION =>CURL_HTTP_VERSION_2_0,
]);
curl_setopt($cURLConnection, CURLOPT_HTTPHEADER, array(
'path: /3/device/<devicetoken>',
'authorization: bearer ' . $token,
'Content-Type: application/json'
));
curl_setopt($cURLConnection, CURLOPT_POSTFIELDS, $postRequest);
curl_setopt ($cURLConnection, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
$apiResponse = curl_exec($cURLConnection);
but the APNS server always returning 'Empty reply from server'
I see a few things that could be problematic.
Try adding the following to actually turn your request into a POST request.
curl_setopt($c, CURLOPT_POST, 1);
Your 'path' should also be part of the main URL instead of being added as a header.
curl_setopt($c, CURLOPT_URL, "https://api.push.apple.com:443/3/device/<devicetoken>");
In addition to the previous suggestion, we should also be able to use certificate bases auth system.
Example snippet below:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://path-to-auth-example.com/token');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_cert");
curl_setopt($ch, CURLOPT_SSLCERT, "path/to/your/pem-file.pem");
curl_setopt($ch, CURLOPT_SSLCERTTYPE, "PEM");
$headers = [];
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
//catch your error wisely
}
curl_close($ch);
$result = json_decode($result);
print_r($result);
?>
I'm creating the authentication and getting an access token fine but the problem arises when I try to send a request. I'm creating the headers using PHP
<?php
define("POST", "POST");
**$environment=' https://api-crt.cert.havail.sabre.com';**
$userId='XXXXXXX';
$domain='AA';
$Group='XXXXXXXXXX';
$formatVersion='V1';
$clientSecret=base64_encode('XXXXXXXXXXX');
$client_id=base64_encode($formatVersion.":".$userId.":".$Group.":".$domain);
$bulid_credential=base64_encode($client_id.":".$clientSecret);
######################## Step 1: Get Token #############################
**$ch =curl_init("https://api-crt.cert.havail.sabre.com/v2/auth/token");**
$vars ="grant_type=client_credentials";
$header =array(
'Authorization: Basic '.$bulid_credential,
'Accept: */*',
'Content-Type: application/x-www-form-urlencoded'
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$res= curl_exec($ch);
$result=json_decode($res);
$token=$result->access_token;
########################## Step 2: Call the REST API###################
**$url="https://api.test.sabre.com/v3.3.0/shop/flights?mode=live";**
$header = array(
'Authorization: Bearer'. $token,
'Content-Type: application/json'
);
$calltype='POST';
$ch = curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $calltype);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonrequest);//Request
array_push($header, 'Content-Type: application/json');
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$result=curl_exec($ch);
$result1=json_decode($result);
curl_close($ch);
?>
The Sabre Response :
{"status":"Incomplete","type":"Application","errorCode":"ERR.2SG.PROVIDER_ERROR","timeStamp":"2017-10-11T06:02:08.658-05:00","message":"Error occurred while invoking service rest:readMetadata:1.13.7.SNAPSHOT"}
1) Did you ensure that $token is a string and not an object?
I use this:
$result=json_decode($res, TRUE);
$token=$result['access_token'];
Instead of this:
$result=json_decode($res);
$token=$result->access_token;
2) Did you ensure that the content of $jsonrequest is valid?
Maybe it's not the PHP code that is incorrect, but your JSON query.
I have a situation where i want to use curl functionality to get data by api call.
the curl command i'm interested to convert look like this:
"curl -X POST \\ \n'http://test.payumoney.com/payment/op/getPaymentResponse?merchantKey=40747T&merchantTransactionIds=396132-58876806' \\ \n -H 'authorization: KpNTiy57L6OFjS2D3TqPod8+6nfGmRVwVMi5t9jR4NU= \\ \n -H 'cache-control: no-cache' \\ \""
I have tried this much
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://test.payumoney.com/payment/op/getPaymentResponse');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$array('merchantKey'=>40747,'merchantTransactionIds'=>396132-58876806))
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
please help me to correct above code!
thank in advance
You are missing a semicolon in your code and you don't need to declare the array as a variable.
curl_setopt($ch,
CURLOPT_POSTFIELDS,
array('merchantKey'=>40747, 'merchantTransactionIds'=>396132-58876806));
PHP Code
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://test.payumoney.com/payment/op/getPaymentResponse');
curl_setopt($ch, CURLOPT_POST, true);
// Add authorization header
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'authorization: KpNTiy57L6OFjS2D3TqPod8+6nfGmRVwVMi5t9jR4NU=',
));
// Remove $ from array()
curl_setopt($ch, CURLOPT_POSTFIELDS, array('merchantKey'=>40747, 'merchantTransactionIds'=>396132-58876806))
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// For No-Cache
curl_setopt($curl1, CURLOPT_FRESH_CONNECT, TRUE);
$result = curl_exec($ch);
?>
You should do it like this, here i am assuming all your parameters are correct. You were missing some required parameter which were sending in curl command but not in PHP curl that is CURLOPT_HTTPHEADER and CURLOPT_HEADER and you should use http_build_query while sending array in CURLOPT_POSTFIELDS and You should use CURLOPT_FOLLOWLOCATION because your current is URL is redirecting from HTTP to HTTPS
<?php
ini_set('display_errors', 1);
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, "http://test.payumoney.com/payment/op/getPaymentResponse?merchantKey=40747T&merchantTransactionIds=396132-58876806");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('merchantKey'=>"40747",'merchantTransactionIds'=>"396132-58876806")));
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('cache-control: no-cache',
'authorization: KpNTiy57L6OFjS2D3TqPod8+6nfGmRVwVMi5t9jR4NU='));
curl_exec($ch);
I'm trying to use PHP and cURL to request an auth token from PayPal to create a payment.
my code is:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'Accept-Language: en_US'
));
curl_setopt($ch, CURLOPT_USRPWD, 'xxxx:xxxx');
curl_setopt($ch, CURLOPT_POSTFIELDS, 'grant_type=client_credentials');
$output = curl_exec($ch);
?>
I get the response:
curl_setopt() expects parameter 2 to be long, string given in Users/anth/sites/abyss/auth.php on line 11
{"error":"invalid_client","error_description":"Invalid client credentials"}
line 11 is:
curl_setopt($ch, CURLOPT_USRPWD, 'xxxx:xxxx');
I have checked my Client ID and Secret and they are fine.
Does anyone know if I am using the wrong syntax or something?
Thanks
You have used the curl_setopt($ch, CURLOPT_USRPWD, 'xxxx:xxxx') instead of curl_setopt($ch, CURLOPT_USERPWD, 'XXXX:XXXX'); . Note the missed 'E' and apart from that you need to add one more line :
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
So the corrected version is below :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'Accept-Language: en_US'
));
curl_setopt($ch, CURLOPT_USERPWD, 'XXXX:XXXX');
curl_setopt($ch, CURLOPT_POSTFIELDS, 'grant_type=client_credentials');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$output = curl_exec($ch);
print_r($output);