Hi I'm trying to POST some data in an array using JSON to receive a response and output the response. So far I have followed all the parameters closely but it fails to fetch the data.
I am using the Coinbase API to 'generate' a button
https://coinbase.com/api/doc/1.0/buttons.html
I have also put the correct API in the $ch variable below as per this page
https://coinbase.com/docs/api/authentication
It fails to fetch anything back. I have posted the correct details to get a response with some data but it fails, any ideas?
Here is my code
<?php
$data = array(
"button" => array(
"name" => "Product Name",
"price_string" => "1.23",
"price_currency_iso" => "USD",
"custom" => "Order 123",
"description" => "Sample description",
"type" => "buy_now",
"style" => "custom_large"
)
);
$json_data = json_encode($data);
$ch = curl_init('https://coinbase.com/api/v1/buttons?api_key=MYAPIKEY');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($json_data))
);
$output = curl_exec($ch);
$result = json_decode($output);
echo $result->button->type;
?>
Quick fix for it will be to disable certificate checking:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
More secure and proper will be to export CA certificate file (certificate of a company that signed site certificate) in X.509 PEM format and use path to it:
curl_setopt($ch, CURLOPT_CAINFO, "/path/to/CA.crt");
You can also use Mozilla certificate database: http://curl.haxx.se/ca/cacert.pem It includes DigiCert High Assurance EV Root CA used on coinbase.com
Related
I am receiving a "INVALID_BODY" error with the message "body could not be parsed as JSON" when sending a curl request through php to create a plaid link token.
I have the header and body formatted this way:
$ch=curl_init("https://development.plaid.com/link/token/create");
$username = array(
"client_user_id"=>"cus_L7tpXAO0PXsPsh"
);
$headers = array(
'Content-type: application/json'
);
$data = array(
'client_id'=>'ID',
'secret'=>'SECRET',
'client_name'=>'Plaid App',
'user'=>$username,
'products'=>'auth',
'country_codes'=>'US',
'language'=>'en',
'webhook'=>'https://webhook.sample.com'
);
$hstring = http_build_query($headers);
$string = http_build_query($data);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$token = curl_exec($ch);
echo $token;
$return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
?>
There is probably a very obvious formatting issue but I can't see it as is. Appreciate any suggestions or criticisms.
I should also mention building out the POST in Postman also gives invalid body error.
I was getting the same error, but for a different reason. The problem in your code is how you are sending your country codes and products. They are expected to be arrays of strings despite the documentation seeming to say otherwise. Also, I don't think you're sending the data in JSON either... Try this:
$data =[
"client_id" => $plaidID,
"secret" => $plaidSecret,
"client_name" => $clientName,
"user" => [
"client_user_id" => $userID
],
"products" => ["auth"],
"country_codes"=>["US"],
"language" => "en"
];
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,"https://".$apiMode.".plaid.com/link/token/create");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch,CURLOPT_HTTPHEADER,["content-type: application/json"]);
curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode((object)$data,JSON_HEX_APOS | JSON_HEX_QUOT ));
$response = curl_exec($ch);
Integration: Active Collab - Zoho Projects
I'm trying to create a new Time entry under a task in Zoho Projects, using the API.
I've already synced up tasks and projects. But I'm having a hard time creating a time entry.
I've tried with php and with postman. On both ends I get the same error:
Response HTTP Status Code : 400
{"error":{"code":6500,"message":"General Error"}}
Here's my request:
$request_parameters = array(
'authtoken' => 'APITOKEN',
'date' => '02-20-2018',
'bill_status' => 'Billable',
'hours' => '02:22'
);
These are being passed as URL parameters.
If I change any of the parameters the error code changes.
I'm using the example provided in the Zoho Project documentation page:
Not sure what's up.
Here's the request URL:
https://projectsapi.zoho.com/restapi/portal/hidden_portal_id/projects/1167980000000355033/tasks/1167980000000355033/logs/?authtoken=hidden_api_token&date=02-20-2018&bill_status=Billable&hours=02%3A22
I've found a similar thread but could not figure it out based on that
Thanks.
Here's the full code:
/* Set the Request Url (without Parameters) here */
$request_url = 'https://projectsapi.zoho.com/restapi/portal/hidden_portal_id/projects/'.$endpoint;
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$request_parameters = array(
'authtoken' => 'hidden_api_key',
'date' => '02-20-2018',
'bill_status' => 'Billable',
'hours' => '02:22'
);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($request_parameters));
//I've added this line because I've found a similar error 6500 thread see the link above
$request_url .= '?'. http_build_query($request_parameters);
/* Here you can set the Response Content Type */
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
/* Let's give the Request Url to Curl */
curl_setopt($ch, CURLOPT_URL, $request_url);
/* Allows Curl to connect to an API server through HTTPS */
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
d($request_parameters,$request_url);
/* Let's get the Response ! */
$response = curl_exec($ch);
$response_info = curl_getinfo($ch);
/* Don't forget to close Curl */
curl_close($ch);
I want to send the login credentials and a pin number as a JSON data and the request token as my http header.
initially it's like this,
{
"Username":"admin",
"Password":"root123",
"PinCode” : "hello321"
}
and I need to post my Request Header token as well.
and if the request is ok, I should get JSON response as follow,
{
"Title": "Mr.",
"Name": "Pushkov",
"Age": "18"
}
I'm trying to do it in cURL PHP. Below is my controller code.
I tried to save my request token to a variable and pass it in header and post username, password and pinnumber as JSON data. if the login success than user info should be displayed. but I'm struggling to move ahead from here. How can I achieve that?
public function send_data() {
$url='http://samplesite.azurewebsites.net/api/member/loginmember';
$ch = curl_init('http://localhost/main');
$data = array("Username" =>$this->input->post('un'), "Password" =>$this->input->post('pw'), "PinCode" =>$this->input->post('PinCode'));
$data_string = json_encode($data);
//echo $data_string;
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);
//var_dump(json_decode($result, true));
$data2=json_decode($result, true);
$ref_id = ( ( is_array( $data2["UserCode"] ) ? implode(", ", $data2["PinCode"]) : $data2["RequestToken"] ) ); // array to string
$acc_t = $data2["RequestToken"];
$uun = $data2["UserCode"];
//echo $acc_t;
// Closing
curl_close($ch);
//print $result ;
}
In your code above, you set a URL to curl_init() and you also passed another one in CURLOPT_URL option. You can't do that in one cURL request.
If you are using OAuth 2.0 you can set the access token to CURLOPT_XOAUTH2_BEARER option.
More information, please refer to PHP manual on cURL Functions.
I have been struggling with the youtube alot.
The problem is, i have made my own php curl script which should connect to the youtube api and send a livechat message to my current stream.
I am so close to finishing this issue but only this 1 last part stops me.
The script i currently use is:
$data = array("snippet" => ["type" => 'textMessageEvent', 'textMessageDetails' => ['messageText' => '<3']], 'livechatid' => '{{livechatid_here}}');
$data_string = json_encode($data);
$ch = curl_init('https://www.googleapis.com/youtube/v3/liveChat/messages?part=snippet&fields=authorDetails%2Ckind%2Csnippet&key={{Here is my key}}');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string),
'Authorization: Bearer {{access_key}} ')
);
$result = curl_exec($ch);
dd($result);
But im constantly getting the following error:
"code": 400,
"message": "snippet.live_chat_id is required"\n
So what i did was i changed 'livechatid' to:
liveChatId (as API tells me)
live_Chat_Id
livechatid
snippet.livechatid
snippet.liveChatId
snippet.live_chat_id
And none of them worked.
Does anybody know how i can fix this?
Ofcourse i have been deleting my keys and access_tokens from the code above.
I have also tried adding them to the header but i still get the same error again and again.
Does anybody know how i can solve this problem?
Put liveChatId inside the snippet field :
$data = array("snippet" => [
"type" => 'textMessageEvent',
'textMessageDetails' => ['messageText' => '<3'],
'liveChatId' => 'YOUR_LIVE_CHAT_ID'
]);
$data_string = json_encode($data);
I have an API in postman. I want to create a CURL Request and get proper response with it. This is my POSTMAN API.
I am successfully getting this response with it.
"{\"Request\":{\"state\":\"Manama\",\"address\":\"406 Falcon Tower\",\"address2\":\"Diplomatic Area\",\"city\":\"Manama\",\"country\":\"BH\",\"fullname\":\"Dawar Khan\",\"postal\":\"317\"},\"Response\":{\"status\":\"Success\",\"code\":100,\"message\":\"Address is verified\"}}"
Now I want to use this API Call inside my PHP Code. I used this code.
$data = array(
'Request' => 'ValidateAddress',
'address' => test_input($form_data->address),
'secondAddress' => test_input($form_data->secondAddress),
'city' => test_input($form_data->city),
'country' => test_input($form_data->country),
'name' => test_input($form_data->name),
'zipCode' => test_input($form_data->zipCode),
'merchant_id' => 'shipm8',
'hash' => '09335f393d4155d9334ed61385712999'
);
$data_string = json_encode($data);
$url = 'myurl.com/';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
curl_close($ch);
$json_result = json_decode($result, true);
echo '<pre>';print_r($json_result);echo '</pre>';
But I can't see my $json_result. It just echoes <pre></pre> in the view. Can anyone guide me? Thanks in advance. I want to get my Response.
UPDATE
I used curl_error and it gives me the following error.
Curl error: SSL certificate problem: self signed certificate in certificate chain
It is Very Simple Just Click on Code you will get the code in php.
you will get the code in many language like php,java,ruby,javascript,nodejs,shell,swift,pythom,C# etc.
Answer updated as per updated question.
There are two ways to solve this issue
Lengthy, time-consuming yet clean
Visit URL in web browser.
Open Security details.
Export certificate.
Change cURL options accordingly.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "/CAcerts/BuiltinObjectToken-EquifaxSecureCA.crt");
Quick but dirty
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
We are configuring cURL to accept any server(peer) certificate. This isn’t optimal from a security point of view.
Excerpt from very detailed and precise article with screenshots for better understanding. Kindly refer the same before actually implementing it in production site.