I am trying to delete a deal using through the API. The code I have written is below , but its not working. I am not able to figure out where to add the Method "DELETE" while making the call.I am not getting any error message in output. Please suggest.
<?php
$api_token = "myapitoken";
$url = "https://api.pipedrive.com/v1/deal?api_token=" . $api_token;
$deal = array(
'id' => 375,
'method' => 'DELETE'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $deal);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
$result = json_decode($output);
?>
Deleting deal on pivedrive can be done using following code
$id= "deal_id";
$url = "https://api.pipedrive.com/v1/deals/". $id ."?api_token=" . $api_token;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
Your api token is a postfield too.
Maybe this way:
$url = "https://api.pipedrive.com/v1/deal";
$deal = array(
'api_token'=> $api_token,
'id' => 375,
'method' => 'DELETE'
);
Related
I'm trying to validate api data with POST request using cURL but getting no response.
API documentation
<?php
$url = "https://widget.packeta.com/v6/api/pps/api/widget/validate";
$data = array(
"Parameters" => array(
"apiKey" => "XXXXXX",
"id" => "9346",
)
);
$encoded = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$resp = curl_exec($ch);
$decoded = json_decode($resp);
print_r($decoded);
curl_close($ch);
?>
Does anyone know what is wrong?
SOLUTION:
Turns out i was missing CURL_HTTPHEADER.
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json",
"Accept: application/json"
));
Try to write:
$ch = curl_init();
instead of :
$ch = curl_init($url);
Eventualy you can use a try ... catch to get the error:
<?php
// Define variables
define('API_KEY', 'XXXXXX');
$url = "https://widget.packeta.com/v6/api/pps/api/widget/validate";
$id = "9346";
// Prepare data
$data = array(
"Parameters" => array(
"apiKey" => API_KEY,
"id" => $id,
)
);
$encoded = json_encode($data);
try {
// Initialize cURL
$ch = curl_init();
// Set cURL options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute cURL request
$resp = curl_exec($ch);
if($resp === false) {
throw new Exception(curl_error($ch));
}
// Decode response and print it
$decoded = json_decode($resp);
print_r($decoded);
// Close cURL session
curl_close($ch);
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
}
?>
I have a PHP script that posts data to another server. It works when I run the script from the command line, but when I run it from a browser I get HTTP code 0 and curl_error() is an empty string. What could cause this?
Both servers are on AWS running CentOS Stream 9 and Nginx.
<?php
$data = array(
'name' => "jack",
'email' => "jack#example.com"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.example.com/register.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($ch);
$httpCode = curl_getinfo($ch , CURLINFO_HTTP_CODE);
if($res === false) {
echo "<p>curl_error: ".curl_error($ch)."</p>\n";
}
echo stripslashes($res);
echo "\n<p>http code: ".$httpCode."</p>\n";
curl_close($ch);
?>
Can you try like this
<?php
$data = array(
'name' => "jack",
'email' => "jack#example.com"
);
$ch = curl_init('https://www.example.com/register.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($ch);
$httpCode = curl_getinfo($ch , CURLINFO_HTTP_CODE);
if($res === false) {
echo "<p>curl_error: ".curl_error($ch)."</p>\n";
}
echo stripslashes($res);
echo "\n<p>http code: ".$httpCode."</p>\n";
curl_close($ch);
?>
I think it should properly.
I need to create a local host using google client api . I wrote following code to crate local post.
$service = new Google_Service_MyBusiness($client);
$callToAction = new Google_Service_MyBusiness_CallToAction();
$callToAction->setActionType('LEARN_MORE');
$callToAction->setUrl('localhost.com');
$mediaItem = new Google_Service_MyBusiness_MediaItem();
$mediaItem->setMediaFormat('PHOTO');
$mediaItem->setSourceUrl('https://www.theanthem.com/images/usps_eddm_postcards.jpg');
$localPost = new Google_Service_MyBusiness_LocalPost();
$localPost->setLanguageCode('en-US');
$localPost->setSummary('Just a summary');
$localPost->setCallToAction($callToAction);
$localPost->setMedia($mediaItem);
$parent = sprintf('accounts/%d/locations/%d',
'116633170334837786295',
'8830395735149945322'
);
$service->accounts_locations_localPosts->create($parent, $localPost);
But Unfortunately I got following error.
My Error: Message: Error calling POST
https://mybusiness.googleapis.com/v4/accounts/9223372036854775807/locations/8830395735149945322/localPosts:
(400) Request contains an invalid argument.
How can i fix this?
I found the answer:
create refresh token
$request_url = "https://www.googleapis.com/oauth2/v4/token";
$refresh_token = "*** Refresf Token ***";
$params = [
'client_id' => "***your client id***",
'client_secret' => "***your clinet secret id***",
'refresh_token' => $refresh_token,
'grant_type' => "refresh_token"
];
$curl = curl_init($request_url);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HEADER,'Content-Type: application/x-www-form-urlencoded');
$postData = "";
//This is needed to properly form post the credentials object
foreach($params as $k => $v) {
$postData .= $k . '='.urlencode($v).'&';
}
$postData = rtrim($postData, '&');
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
$json_response = curl_exec($curl);
$response = (array) json_decode( $json_response );
$myaccess_token = $response['access_token'];
And create Post
$location = 'accounts/accoutid/locations/location id';
$api_end_point_url = 'https://mybusiness.googleapis.com/v4/'.$location.'/localPosts';
$postfields = array(
'topicType' => "STANDARD",
'languageCode' => "en_US",
'summary' => 'test post 123',
);
$data_string = json_encode($postfields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_end_point_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '. $myaccess_token,'Content-Type: application/json'));
$data1 = json_decode(curl_exec($ch), true);
$http_code1 = curl_getinfo($ch, CURLINFO_HTTP_CODE);
I am trying to call changelly API with below codes but it is returning "Unauthorized" in response.
Appreciate if someone can help in identifying the mistake I am making in below code.
$API_URL = 'https://api.changelly.com';
$API_KEY = 'XXXXX';
$API_SECRET = 'XXXXX';
$message = array();
$message['jsonrpc'] = '2.0';
$message['method'] = 'getMinAmount';
$message['params'] = array('from' => 'BTC', 'to' => 'LTC');
$message['id'] ='12345';
$data = json_encode($message);
$sign = hash_hmac('SHA512', $data, $API_SECRET);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $API_URL);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_GET, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/json",'Authorization:api-key: '.$API_KEY.':sign: '.$sign));
$final_result = curl_exec($ch);
curl_close($ch);
echo '<pre>';
print_r($final_result);
Changelly API guide is at https://changelly.com/developers
Thanks
in your code you have wrong set of headers.
please check this example: https://github.com/changelly/changelly-examples/blob/master/php/example.php, hope it helps.
I tried to parse this url
https://esewa.com.np/epay/transdetails?pid=AddFund-C-11970239- 9625960&amt=100&scd=nprhosting&rid=00C3LF0
{
"code":"00",
"msg":"Success",
"txnDetail": {
"txnCode":"00C3LF0",
"amt":"100.0",
"date":"2015-07-16 23:44:18.0",
"payerId":"dipsnwc#gmail.com",
"status":"COMPLETE",
"pid":"AddFund-C-11970239-9625960",
"txAmt":"0",
"psc":"0",
"pdc":"0"
}
}
Like this
$fields = array(
'pid' => "AddFund-C-11970239-9625960";
'amt' => "100.0";
'scd' => "nprhosting";
'rid' => "00C3LF0";
);
$field2 = json_encode($fields);
$url = "https://esewa.com.np/epay/transdetails";
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $field2);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($field2))
);
// Execute post
$result = curl_exec($ch);
// Close connection
curl_close($ch);
//
///Deocde Json
$data = (json_decode($result, true));
var_dump($data);
$message =$data['msg'];
$status =$data['txnDetail']['status'];
echo $message;
echo $status;
Still no output ??
I tried it and worked..
$url = 'https://example.com/epay/transdetails?pid=AddFund-C-11970239-9625960&amt=100&scd=nprsite&rid=00C3LF0';
$data = file_get_contents($url);
$arr = json_decode($data,true);
echo $arr['txnDetail']['status'];
print_r($arr);
Array is incorrect, remove ::
$fields = array(
'pid' => "AddFund-C-11970239-9625960",
'amt' => "100.0",
'scd' => "nprhosting",
'rid' => "00C3LF0"
);
and try
$url = "http://examplesite.com/epay/transdetails?" . http_build_query($fields);
Chuck the POSTFIELDS and HTTPHEADER
curl_setopt($ch, CURLOPT_POST, false);
The parameters are expected to be as GET(as per the link you have provided), keep it simple.
Also check this answer for better understanding on how to send HTTP GET request with PHP CURL.