I am trying to access an API using PHP, for which I have the cert.pem, key.pem, and an encryption key.
I suspect that my authorization column in this code is not correct, so I keep getting an Authorization field missing, or that I do not have access to this API. But I do have the access.
Secondly, for the key.pem, should I write it as CURLOPT_CAINFO or CURLOPT_SSLKEY.
Please suggest what step is missing or incorrect in this code below.
$url = "API endpoint url";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array(
"Accept: application/json",
"Authorization: ",
);
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYHOST => '2',
CURLOPT_CAINFO => 'key.pem',
CURLOPT_SSLCERT => 'cert.pem'
));
$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);
Related
I have try curl request with terminal it's working but when i convert that curl request into php code that one passing param not working.
Terminal curl request :
curl --insecure "https://www.zohoapis.in/phonebridge/v3/clicktodial" -X POST -d "clicktodialuri=$clicktodialurl&clicktodialparam=[{'name':'fromnumber','value':'555'}]&zohouser=123456" -H "Authorization: Zoho-oauthtoken 1000.aedb399e2389cfacef60f965af052cbf" -H "Content-Type: application/x-www-form-urlencoded"
Response :
{"message":"ASTPP Clicktodial functionality has been enabled","status":"success","code":"SUCCESS"}
PHP Code :
$zohouser = '6000';
$access_token = '1000.c3c1107b635f1f5b257d831677e077d2';
$cURL = "https://www.zohoapis.in/phonebridge/v3/clicktodial?clicktodialuri=$click_to_dial&clicktodialparam=[{'name':'fromnumber','value':'555'}]&zohouser=$zohouser";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $cURL,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => array(
"Authorization: Zoho-oauthtoken " . $access_token,
"Content-Type: application/x-www-form-urlencoded",
"cache-control: no-cache"
),
));
$response = json_decode(curl_exec($curl));
$err = curl_error($curl);
print_r($err);
curl_close($curl);
print_r($response);exit;
Not getting any response or error during run this php curl request.
Can you please help me how to pass string as param in php curl request.
$strURL= "https://www.zohoapis.in/phonebridge/v3/clicktodial";
$arrHeader= array(
'Authorization:Zoho-oauthtoken 1000.aedb399e2389cfacef60f965af052cbf'
);
$params= array(
"clicktodialparam"=>"[{\"name\":\"fromnumber\",\"value\":\"555\"}]",
"authorizationparam"=>"{\"name\":\"X-Auth-Token\",\"value\":\"1000.aedb399e2389cfacef60f965af052cbf\"}",
"clicktodialuri" => "$click_to_dial",
"zohouser" => "123456"
);
$ch= curl_init();
curl_setopt_array($ch,array(
CURLOPT_URL =>$strURL,
CURLOPT_POST => 1,
CURLOPT_HTTPHEADER => $arrHeader,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => 0,
CURLOPT_TIMEOUT => 0,
CURLOPT_POSTFIELDS => http_build_query($params)
));
$strResponse= curl_exec($ch);
print_r($strResponse);
echo curl_error($ch);
Referring to this post and using the linked tool we end up with the following code. Since I cannot test this myself I cannot guarantee my answer. It looks like you might be missing the curl post option "curl_setopt($ch, CURLOPT_POST, 1);" which you can use in lieu of the option you used "CURLOPT_CUSTOMREQUEST => "POST"".
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.zohoapis.in/phonebridge/v3/clicktodial');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "clicktodialuri=$clicktodialurl&clicktodialparam=[{'name':'fromnumber','value':'555'}]&zohouser=123456");
$headers = array();
$headers[] = 'Authorization: Zoho-oauthtoken 1000.aedb399e2389cfacef60f965af052cbf';
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
I recently got a new job. They need me to get information from ShipStation through the API using PHP. I'm fairly new to PHP and even newer to ShipStation. I copied the code from the API documentation and attempted to add the code for authorization. This is what I've got:
<?php
$apiKey = "my_api_key";
$apiSecret = "my_api_secret";
$auth = base64_encode($apiKey . ":" . $apiSecret);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://ssapi.shipstation.com/orders");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Authorization: Basic " . $auth
));
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
?>
Instead of giving me order information it's just returning bool(false).
I guess I'm just not seeing what I'm doing wrong. Any help would be greatly appreciated. Thanks!
This is coming up in searches for the query "Shipstation API PHP" so I wanted to point to this answer How do I make a request using HTTP basic authentication with PHP curl? and reiterate that it's a basic auth so you just need to do
curl_setopt($ch, CURLOPT_USERPWD, $apiKey . ":" . $apiSecret);
I'd also recommend sending it over a secure connection to avoid man in the middle reading your API credentials.
I connect with CURL this way:
$url = 'https://ssapi.shipstation.com/orders';
$ShpStnAuth = 'Authorization: basic '.base64_encode('key:pair');
$curl = curl_init();
curl_setopt_array(
$curl, array(
CURLOPT_URL => $url,
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(
'Host: ssapi.shipstation.com',
$this->ShpStnAuth,
),
)
);
$response = curl_exec($curl);
curl_close($curl);
print_r($response);
I tested an API using postman. Using, postman, I managed to generate a token.
From postman, I managed to get all the data of a user using the following with the following details via a GET request:
https://example.example.co.za/api/consumers/get?id=567675675&email=example#sample.com
Under Authorization type, I selected "Bearer token" and pasted the Token in its respective field.
When I click send, I get a success response with the user data.
In PHP, how can I do the same api call (using the id,email and token) inside a php function?
I tried this:
curl_setopt_array($curl, array(
CURLOPT_URL => "example.example.co.za/api/consumers/get?email=example#sample.com&id=567675675",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => "",
CURLOPT_HTTPHEADER => array(
"Accept: */*",
"Authorization: Bearer jhgukfytjfytdytfjgjyfytfjkugfyfdhtklhkugjf",
"Cache-Control: no-cache",
"Connection: keep-alive",
"Content-Type: application/x-www-form-urlencoded",
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
return $response;
But when I view the page, instead of the user data, I see this:
How do I make the API call inside a function to get the same result that I have on postman in PHP?
You need to follow the location
CURLOPT_FOLLOWLOCATION => true
https://www.php.net/manual/de/function.curl-setopt.php
Please find a proper CURL call here.
$url = "https://example.example.co.za/api/consumers/get?id=567675675&email=example#sample.com";
$curl = curl_init();
// OPTIONS:
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'APIKEY: 111111111111111111111',
'Content-Type: application/json',
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
// EXECUTE:
$result = curl_exec($curl);
if(!$result){die("Connection Failure");}
curl_close($curl);
I am trying to cURL apptweak (ref - https://apptweak.io/api )
curl -H 'X-Apptweak-Key: your-api-key' https://api.apptweak.com/ios/applications/284882215.json
I have my key and can curl from the terminal. In PHP, I get "authorization token missing".
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://api.apptweak.com/ios/applications/284882215.json&country=US&language=en',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
X-Apptweak-Key => 'MY-KEY-IS-HERE'
)
));
$resp = curl_exec($curl);
print $resp;
curl_close($curl);
Is X-Apptweak-Key => 'MY-KEY-IS-HERE' being a POST field the issue here?
What is wrong?
you can add X-Apptweak-Key between single quotes it's a key
CURLOPT_POSTFIELDS => array(
'X-Apptweak-Key' => 'MY-KEY-IS-HERE'
)
or you can try this:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8',
'Authorization: Basic MY-KEY-IS-HERE'));
or you can use:
curl_setopt($ch, CURLOPT_USERPWD, "X-Apptweak-Key:MY-KEY-IS-HERE");
I want to make a cURL request to a URL along with the following headers :
'Content-Type: application/json',
'Authorization': 'Basic XXXXXXXXXX'
I have the following code :
<?php
$post_url = "https://api.interlinkexpress.com/user/?action=login";
$curl = curl_init($post_url);
$headers = array(
'Content-Type: application/json',
'Authorization': 'Basic XXXXXXXXX'
);
//curl_setopt($curl, CURLOPT_USERPWD, "username":"Password");
curl_setopt($curl, CURLOPT_URL, $post_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, true);
//curl_setopt($curl, CURLOPT_POSTFIELDS,json_encode($post_data) );
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
$post_response = curl_exec($curl);
?>
it is showing the result as :
Client sent a Bad Request
which is the 'Authorization...' line under the $header array
Any suggestions/help is appreciated.
problem is in header.It needs to be fixed.authorization will e whole string like this:
$headers = array(
'Content-Type: application/json',
'Authorization: Basic XXXXXXXXX'
);
let me give you a full example of curl:
function CurlSendPostRequest($url,$request)
{
$authentication = base64_encode("username:password");
$ch = curl_init($url);
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => false, // follow redirects
// CURLOPT_ENCODING => "utf-8", // handle all encodings
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 20, // timeout on connect
CURLOPT_TIMEOUT => 20, // timeout on response
CURLOPT_POST => 1, // i am sending post data
CURLOPT_POSTFIELDS => $request, // this are my post vars
CURLOPT_SSL_VERIFYHOST => 0, // don't verify ssl
CURLOPT_SSL_VERIFYPEER => false, //
CURLOPT_VERBOSE => 1,
CURLOPT_HTTPHEADER => array(
"Authorization: Basic $authentication",
"Content-Type: application/json"
)
);
curl_setopt_array($ch,$options);
$data = curl_exec($ch);
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
//echo $curl_errno;
//echo $curl_error;
curl_close($ch);
return $data;
}
500 Error typically suggests that the error is on the server side. You can probably try to emulate your request ( I would personally recommend using Advanced ReST Client, which is a google-chrome app), to verify whether this is the case.