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);
Related
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);
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 need to request an api with curl in PHP.
First of all I need to get a token access, and to get it I need to request to this url (with my "client_id") : https://testexellia.bitrix24.fr/oauth/authorize/?response_type=code&client_id=local.5d3c9f79b65439.23207420&redirect_uri=app_URL
It should answer me a new url with the token inside.
It works very well when I request with Chrome, but with curl I get this answer -> https://www.bitrix24.net/oauth/authorize/?user_lang=fr&client_id=b24.5d399107d0f2b9.16511658&redirect_uri=https%3A%2F%2Ftestexellia.bitrix24.fr%2Foauth%2Fauthorize%2F%3Fauth_service_id%3DBitrix24Net%26oauth_proxy_params%3DcmVkaXJlY3RfdXJpPWFwcF9VUkwmY2xpZW50X2lkPWxvY2FsLjVkM2M5Zjc5YjY1NDM5LjIzMjA3NDIwJnJlc3BvbnNlX3R5cGU9Y29kZQ%253D%253D%26sessid%3Db6ded2712b7e66e4b34f05411eb2f4f1&scope=auth,profile&response_type=code&mode=page&state=site_id%3Ds1%26backurl%3D%252Foauth%252Fauthorize%252F%253Fcheck_key%253D7d6109a54034f59c406798d6a9667a9c%2526response_type%253Dcode%2526client_id%253Dlocal.5d3c9f79b65439.23207420%2526redirect_uri%253Dapp_URL%26mode%3Dpage
So the login page.
I know where is the problem, I'm already logged with Chrome (probably with cookies I guess) to the website, and obviously the curl request not.
I guess I need to add some headers informations to my request, like a password and login, but I'm logged to this website with my Google Account.
So is there a way to add Google informations in the curl's header ? I can't find how.
Here is the doc of the API, in case you need.
Edit : Here is the code.
$curl = curl_init();
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt_array($curl, array(
CURLOPT_URL => "https://testexellia.bitrix24.fr/oauth/authorize/?response_type=code&client_id=local.5d3c9f79b65439.23207420&redirect_uri=app_URL&sessid=b6ded2712b7e66e4b34f05411eb2f4f1",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Accept: */*",
"Authorization: Bearer 08493f5d003eabc0003e9b9e00000001f0f103a29e30c5356d8e8142404add6515e135",
"Cache-Control: no-cache",
"Connection: keep-alive",
"Host: testexellia.bitrix24.fr",
"Postman-Token: 6de6ba41-b9fc-4541-98b0-c2f9d9b1ea7f,b36cd02a-87f8-4fb1-821c-54602a300bc5",
"User-Agent: PostmanRuntime/7.13.0",
"accept-encoding: gzip, deflate",
"cache-control: no-cache",
"cookie: PHPSESSID=fOJKQhrXBNuNmrmIOXXg0YDlIeckdVfm; qmb=.; BITRIX_SM_SALE_UID=0"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
$content = curl_exec($curl);
$redirectedUrl = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL);
curl_close($curl);
echo $redirectedUrl;
Thank you very much.
Benjamin.
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");
curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"firstname":"Mike","lastname":"Doel","customer_id":"12345","email":"test_api_user#gmail.com.com"}' -u API-key: APIURL(http://)
above statement is running well in command but i am unable to achive the same by php code below is my code
$url="https://apiurl";
$data=array("firstname"=>"Mike","lastname"=>"Doel","customer_id"=>"12345","email"=>"test_api_user#gmail.com");
$data_json=json_encode($data);
//Curl code
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',"Accept: application/json","api-key"));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
In CURL, for api key you need to pass username:password , if you going to access in php code
$url="https://apiurl";
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => 'username:password',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 300000,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => array(
"accept: application/json",
"content-type: application/json"
),
CURLOPT_POSTFIELDS => $postfields,
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
return $response;
Try it once..hopefully it will work for you.
This:
curl_setopt($ch, CURLOPT_HTTPHEADER, array([..snip..], "api-key"));
^^^^^
and
curl [..snip..] -u api-key
^^^^^^^^^^
are NOT equivalent. -u specifies HTTP Basic authentication, with username:password. Your setopt is just stuffing that username as the NAME of an http header, which is not how basic auth credentials show up
You should have
curl_setopt($ch, CURLOPT_USERNAME, "api-key");
instead.