Hi I'm using following code to send Https GET request to another api
$access_token = $this->Session->read("AUTH_BEARER");
$url = GET_DOCUMENT_BY_ID_URL.'202';
$curl=curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, 0);
curl_setopt($curl, CURLOPT_HTTPGET, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Authorization: Bearer ".$access_token));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);// comment this line in live servers
$response_json=curl_exec($curl);
$error= curl_errno($curl);
$error_message= curl_error($curl);
echo "error:".$error;
echo "<br>";
echo "Error message:".$error_message;
echo "<br>";
curl_close($curl);
$response_array = json_decode($response_json,true);
echo $response_json;
But I didn't receive any response from server. I make this call as soon as I receive bearer token, so I hope I made the call before token expire. what I'm doing wrong here? can someone help me to solve this? Thanks in advance.Note: I'm using https:// not http://
nothing to do with cakephp here , if You only want to send GET request , you dont need to use curl , you can use file_get_contents like the following example
$url = 'http://www.example.com/index.php?xxxx=112';
$options = array('http' => array(
'method' => 'GET',
'header' => 'Authorization: Bearer '.$access_token
));
$context = stream_context_create($options);
$webservice= file_get_contents($url, false, $context);
echo $webservice;
Related
I checked the API documentation but there are no examples related to curl php.
Can i get some guide on how to connect with monday.com to create lead or deal in monday.com using curl php?
I have sample code (Token is wrong in this code snippet), but I have no idea on how to pass data to create lead
<?php
$token = 'eyJhbGciOiJIUzI1NiJ9.0Y-0OesftWBt2SamhvuPV5MR-0Oq7iApMt2exFkDNdM';
$apiUrl = 'https://api.monday.com/v2';
$headers = ['Content-Type: application/json', 'Authorization: ' . $token];
$query = '{ boards (limit:1) {id name} }';
$data = #file_get_contents($apiUrl, false, stream_context_create([
'http' => [
'method' => 'POST',
'header' => $headers,
'content' => json_encode(['query' => $query]),
]
]));
$responseContent = json_decode($data, true);
echo json_encode($responseContent);
?>
I'm not familiar with this monday.com page, but this is how you can make a cURL request in PHP:
<?php
$token = 'eyJhbGciOiJIUzI1NiJ9.0Y-0OesftWBt2SamhvuPV5MR-0Oq7iApMt2exFkDNdM';
$apiUrl = 'https://api.monday.com/v2';
$headers = ['Content-Type: application/json', 'Authorization: ' . $token];
// Payload
$query = '{ boards (limit:1) {id name} }';
$payload = ['query' => $query];
// Init cURL
$curl = curl_init();
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($payload));
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_URL, $apiUrl);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
// Exec cURL
$resp = curl_exec($curl);
// Close cURL
curl_close($curl);
// Get response
$response = #json_decode($resp, true);
So my question is the following, using xampp for localhost, using PHP 7+ and curl, i need to make an API post call. it was given to me an API Key, that i don t know how to validate. this is the code:
<?php
$curl = curl_init();
$data_array = array(
"amount" => '1',
"operative"=> "AUTHORIZATION",
"signature"=> "KKbAWfePbyt41T7inU8ulGtR",
"customer_ext_id"=> "IDInterno",
"additional"=> "Additional",
"service"=>"CE4C4D1C-2A2C-4AA7-BE59-62E497AEFE16",
"secure"=> true,
"url_post"=> "https://demo.pagaqui.pt:5135/RegisterCard.aspxOk: ",
"url_ok"=> "https://carteira.pagaqui.pt/repo/ok.htmlKO: ",
"url_ko"=> " https://carteira.pagaqui.pt/repo/nok.html ",
"template_uuid"=> "43D8016C-D28C-4F44-899A-2195CA8DC023",
"description"=> "Desc"
);
$data_array = http_build_query($data_array);
curl_setopt($curl, CURLOPT_URL, 'https://api-gateway.pagaqui.pt/v1/sandbox/payment');
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, ['content-type: application/json']);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_array);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
$err = curl_error($curl);
if($err){
echo 'Curl Error: ' . $err;
}
else {
print_r($response);
}
<?php
My response in the localhost is
{"message":"Unauthorized","code":401,"details":""}
Thank you for the attention.
Already solved it, just needed to create a variable to the key, and after that, inside curl_setopt($curl, CURLOPT_HTTPHEADER, array( "Authorization: Bearer ".$Key, 'Content-Type: application/json' ));
Thank you.
I have this curl output where I replace with my id, secret and url. It gives correct response.
curl -XPOST -H "Cache-Control:no-cache" -H "Content-Type:application/json" --user 'id:secret' 'url'
But when I'm trying to write it in php in the following way, it's giving error for invalid cleint. How to rewrite curl from the above output in the same way in php? Thanks!
$url = 'https://test.api.neteller.com/v1/oauth2/token?grant_type=client_credentials';
$clientId = 'client_id;
$clientSecret = 'secret_id';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERPWD, $clientId . ":" . $clientSecret);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);
It's giving error - 'invalid cleint', but when I run it in console, it returns access token. This code is only for access token, not for payment.
Edited: My current code is:
$url ='https://test.api.neteller.com/v1/oauth2/token?grant_type=client_credentials';
$clientId = NETELLER_CLIENT_ID;
$clientSecret = NETELLER_CLIENT_SECRET;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array(
'Authorization: Basic '. base64_encode($clientId) . ':' . base64_encode($clientSecret),
'Content-Type:application/json'
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($curl);
if (curl_error($curl)) {
curl_close($curl);
throw new \Exception('System error. Can not get neteller token');
}
curl_close($curl);
Tried to add the credentials as part of the headers.
$headers = array(
'Authorization: Basic '. base64_encode($clientID . ':' . $clientSecret),
'Content-Type:application/json'
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
Ok I found out.
Update:
There are 4 reasons why you may get { "error": "invalid_client" } when trying to run your code.
You are using client_id + client_secret from your test account on the production environment or vice versa.
Your client_id or client_secret values are wrong.
The IP address from which your application is making outgoing requests is not white-listed in your merchant account.
You are not sending the Authorization header properly.
You missed curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type:application/json", "Cache-Control:no-cache")); from your code.
I've been looking around for the past few days for an answer to this question, but I haven't been able to find the solution I'm after.
I have a fully working version of doing this task using Google_Client, however I want to be able to do this without using the Google_Client. I can create buckets without using it, but not Objects. I cannot understand the Google Documentation on this at all (it's pretty poor).
So, I have a function below which takes in several parameters to try and upload a file. I'm not sure what needs to go in $authheaders, $post_fields (the body of the post) or the url.
public function upload_file($bucket, $fileName, $file, $fileType) {
// Check if we have auth token
if(empty($this->authtoken)) {
echo "Please login to Google";
exit;
}
// Prepare authorization headers
$authheaders = array(
"Authorization: Bearer " . $this->authtoken
);
$postbody = array();
//Http call for creating a file
$response = $this->http_call(self::FILE_UPLOAD_URL.$bucket.'/o?uploadType=multipart&name='.$fileName, $postbody, $authheaders);
// Has the file been created successfully?
if($response->success=="1") {
return array('status' => 'success', 'errorcode' =>'', 'errormessage'=>"", 'id' => $response->job->id);
}
else {
return $response;
}
}
The http_call function:
public function http_call($url, $post_fields, $authheaders) {
// Make http call
$this->httpRequest->setUrl($url);
$this->httpRequest->setPostData(json_encode($post_fields));
$this->httpRequest->setHeaders($authheaders);
$this->httpRequest->send();
$response = json_decode($this->httpRequest->getResponse());
return $response;
}
Any help on this would be greatly appreciated.
If anyone else is looking to do this without using the API, here is how I solved my question using curl.
// Prepare authorization headers
$authheaders = array(
"Authorization: Bearer " . $this->authtoken,
"Content-Type: application/pdf"
);
$url = self::FILE_UPLOAD_URL.$bucket.'/o/?uploadType=multipart&name='.$fileName.'';
$curl = curl_init();
curl_setopt($curl, CURLOPT_POSTFIELDS, $file);
curl_setopt($curl, CURLOPT_HTTPHEADER, $authheaders);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_USERAGENT, "Goole Cloud Storage PHP Starter Application google-api-php-client/1.1.5");
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
// 1 is CURL_SSLVERSION_TLSv1, which is not always defined in PHP.
curl_setopt($curl, CURLOPT_SSLVERSION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
$response = curl_exec($curl);
Not being able to authenticate Google Spreadsheet API.
I'm using this code stolen from here,
$clientlogin_url = "https://www.google.com/accounts/ClientLogin";
$clientlogin_post = array(
"accountType" => "HOSTED_OR_GOOGLE",
"Email" => "MY_EMAIL#GOOGLE.COM",
"Passwd" => "MY_EMAIL_PASS",
"service" => "writely",
"source" => "MY_APPLICATION_NAME"
);
// Initialize the curl object
$curl = curl_init($clientlogin_url);
// Set some options (some for SHTTP)
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $clientlogin_post);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// Execute
$response = curl_exec($curl);
// Get the Auth string and save it
preg_match("/Auth=([a-z0-9_\-]+)/i", $response, $matches);
$auth = $matches[1];
echo "The auth string is: " . $auth; //this worked!
So this works and now I have a key But actually using it...with the bellow code gives me a user not authenticated error:
$headers = array(
"Authorization: GoogleLogin auth=" . $auth,
"GData-Version: 3.0",
);
$key = 'MY_KEY';
// Make the request
curl_setopt($curl, CURLOPT_URL, 'https://spreadsheets.google.com/tq?tqx=version:0.6;out:json&key='.$key);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, false);
$response = curl_exec($curl);
curl_close($curl);
var_dump ($response);
this gives me
string(272) "google.visualization.Query.setResponse({version:'0.6',status:'error',errors:[{reason:'user_not_authenticated',message:'User not signed in',detailed_message:'\u003ca target=\u0022_blank\u0022 href=\u0022http://spreadsheets.google.com/\u0022\u003eSign in\u003c/a\u003e'}]});"
So apparently it is not able to use the authentication key for the Google Visualization Query Language.. what am I doing wrong?
Thank you so much!
I would try the following modifications in the code that uses google authentication string:
add urlencode
$headers = array(
"Authorization: GoogleLogin auth=" . urlencode($auth),
"GData-Version: 3.0",
);
and skip verifying SSL sertificates:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
I've described my experience implementing authentication with CURL in this post. There you'll find some tips on debugging CURL requests.