I'm trying to get a range query to work with PHP cURL and Cloudant.
I've setup the following search index in my Cloudant:
{
"_id": "_design/date",
"_rev": "20-822ebda694f4fb57dc0a48bf60f6570f",
"views": {},
"language": "javascript",
"indexes": {
"by_date": {
"analyzer": "classic",
"index": "function (doc) {\n index(\"default\", doc.datequery);\n}"
}
}
}
And then in my PHP document I do the following:
$urlstuff = "[20180110 TO 20180119]";
$url = "https://user:pass.#user.cloudant.com/db/_design/date/_search/by_date?q=".$urlstuff."&include_docs=true&limit=200";
$ch = curl_init(); // initialize curl handle
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$output = curl_exec($ch);
And I get Bad Request.
If I replace:
$urlstuff = "[20180110 TO 20180119]";
for
$urlstuff = "20180117";
Then I get good results.
Is it the square brackets messing with my cURL?
What am I missing?
Yes, you are on the right track. If you escape your variable (e.g. using http://php.net/manual/en/function.curl-escape.php?) your query should work:
curl -g -X GET 'https://user:password#user-bluemix.cloudant.com/db/_design/date/_search/by_date?q=[20180110 TO 20180119]&include_docs=true&limit=20'
{"error":"bad_request","reason":"Bad request"}
vs
curl -X GET 'https://user:password#user-bluemix.cloudant.com/db/_design/date/_search/by_date?q=%5B20180110%20TO%2020180119%5D&include_docs=true&limit=20'
{"total_rows":1, ...
Related
So I am trying to display info from a website onto my website via an API using curl.
And I am not to clued up on curl at all :(
So this is what I have to from the source website :
curl -H "IHOSTMN-API-KEY:YourIhostmnApiKeyHere" -X GET "https://ihostmn.com/api/v1/sharing/user/list_all_deposits"
And it should give this response:
{
"result": {
"deposits": [
{
"deposit_id": 36,
"share_id": 2,
"current_amount": 53.077553,
"total_deposited": 24.91567,
"total_earned": 28.146885,
"withdraw_address": "GcUgnY5fFwTv9ef8rsggdxbdQzLEUxpp4c",
"deposit_address": "GfUgnY5sFwTf9ez8rovtdxdEQzLcbxpb4c"
},
{
"deposit_id": 37,
"share_id": 5,
"current_amount": 885.9591,
"total_deposited": 521.92,
"total_earned": 472.30566,
"withdraw_address": "gHWHPs21H8UsSNcbfWxvn5XssAxFkcuZYe",
"deposit_address": "g4sbWWtD3tf16Dsd8wiaJkar3zhJ82qNKP"
},
{
"deposit_id": 38,
"share_id": 6,
"current_amount": 754.5115,
"total_deposited": 548.52997,
"total_earned": 416.25214,
"withdraw_address": "LLqWFFJkNSog6VwsbPWEWE4KcXJrzB6t1K",
"deposit_address": "LW5Vbt1gEkvVQzfVcpLmidLPpcED1Yp3yu"
}
]
},
"error": ""
}
I have created my php webpage but can't make it work, this is what wrote:
curl -H "IHOSTMN-API-KEY:xxxxxxxxxxxxxxxxxxxxxxxxx" -X GET "https://ihostmn.com/api/v1/sharing/user/list_all_deposits"
$obj = json_decode($result);
echo $obj[0]->deposits;
Hopefully one of you will be able to point me in the correct direction, and thank you for taking the time to help if you can.
Mark
Try something like this
<?
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://ihostmn.com/api/v1/sharing/user/list_all_deposits');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
$headers = array();
$headers[] = 'Ihostmn-Api-Key: YourIhostmnApiKeyHere';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
$obj = json_decode($result);
print_r($obj->result->deposits);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
you use [] for arrays and arrows for objects.
I cannot test the API but this should give you a headstart.
Helpful links: cURL
I have a working cURL request I want to translate to PHP code:
curl \
-X POST \
-u live_abcdefg: \
-d '{
"recipient": {
"address": "test1#test.com"
}
}' \
https://api.sendwithus.com/api/v1/drip_campaigns/abcdefgh/activate
Here is the PHP code I'm trying:
<?php
$theurl = "https://api.sendwithus.com/api/v1/drip_campaigns/abcdefgh/activate";
$ch = curl_init($theurl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); // -X
curl_setopt($ch, CURLOPT_POSTFIELDS, '{ "recipient": { "address": "test2#test.com" } }'); // -d
curl_setopt($ch, CURLOPT_PROXYUSERPWD, 'live_abcdefg:'); // -u
$response = curl_exec($ch);
var_dump($response); // prints bool(true)
?>
EDIT: Sendwithus (the app that I'm sending requests to) shows that the second request didn't come through (second recipient test2#test.com wasn't saved).
What am I doing wrong?
What can I change in the PHP code to start debugging this issue?
I'm trying to post data to a JIRA REST API but I get a blank response back. The code sits in two files. Here's the code located on the jira.json file:
{
"fields": {
"project":
{
"id": "19600"
},
"summary": "REST ye merry gentlemen.",
"description": "Creating of an issue using project keys and issue type names using the REST API",
"issuetype": {
"name": "Task"
}
}
}
and the php code:
<?php
$jsondata = file_get_contents("jira.json");
$json = json_decode($jsondata,true);
//print_r($json);
$url_send ="http://jira.greentea.co.za:8091/rest/api/2/issue/";
$str_data = json_encode($json);
//print_r($str_data);
function sendPostData($url, $post){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($ch);
curl_close($ch);
if($result)
{
return 1;
}
else{
return 0;
}
}
$time = sendPostData($url_send, $str_data);
var_dump($time);
?>
I get a zero int(0) meaning that the result variable returned a blank result. This is the the cURL Command that I converted to php. data will be replaced by the json array:
curl -D- -u fred:fred -X POST --data {see below} -H "Content-Type: application/json" http://localhost:8090/rest/api/2/issue/
If you are sure that you have CURL enabled with your PHP, try to use this snippet from my code:
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json','Content-Length: ' . strlen($json_data)));
Let me know if this works for you.
I'm trying to do a GET request to the XBMC api in PHP, but i cant get it to work. I already have a valid url, thats working when i simulate a GET request with a REST client.
http://localhost:8085/jsonrpc?request={"jsonrpc": "2.0", "id": 1, "method": "Input.ExecuteAction", "params": {"action":"left"}}
I get a parse error from xbmc when i simulate a POST:
{
"error": {
"code": -32700,
"message": "Parse error."
},
"id": null,
"jsonrpc": "2.0"
}
This is the code i currently have:
<?php
$jsonString = "{\"jsonrpc\": \"2.0\", \"id\": 1, \"method\": \"Input.ExecuteAction\", \"params\": {\"action\":\"left\"}}";
$url = "http://localhost:8085/jsonrpc?request=".$jsonString;
get($url);
function get($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT , 7);
$result = curl_exec($ch);
print $result;
curl_close($ch);
}
But this wont do the job, just like when i simulate a POST request a parse error is returned.
So my thoughts are that i'm doing a POST request, am i?
If no, what would be the problem then?
I just ran into the same problem.
You need to encode the url you are using for XBMC (Kodi nowadays). So in your case:
$jsonString
should be replaced by
urlencode($jsonString)
I have the following code
curl -v https://api.sandbox.paypal.com/v1/payments/payment \
-H 'Content-Type:application/json' \
-H 'Authorization:Bearer EEwJ6tF9x5WCIZDYzyZGaz6Khbw7raYRIBV_WxVvgmsG' \
-d '{
"intent":"sale",
"redirect_urls":{
"return_url":"http://example.com/your_redirect_url/",
"cancel_url":"http://example.com/your_cancel_url/"
},
"payer":{
"payment_method":"paypal"
},
"transactions":[
{
"amount":{
"total":"7.47",
"currency":"USD"
}
}
]
}'
I have been trying to convert it, but I don't know which parameters to use.
How can I convert it to PHP?
Looks like you'd need to do this. Note that because you're passing a JSON header, PayPal is (likely) requiring the body data to be sent as JSON, rather than form/value pairs. If you're passing a complex set of data, you may find it easier to define $data as an array, and use json_encode($data) to transform it for the CURLOPT_POSTFIELDS property.
$header = array();
$header[] = 'Content-type: application/json';
$header[] = 'Authorization: Bearer EEwJ6tF9x5WCIZDYzyZGaz6Khbw7raYRIBV_WxVvgmsG';
$url = 'https://api.sandbox.paypal.com/v1/payments/payment';
$data ='{
"intent":"sale",
"redirect_urls":{
"return_url":"http://example.com/your_redirect_url/",
"cancel_url":"http://example.com/your_cancel_url/"
},
"payer":{
"payment_method":"paypal"
},
"transactions":[
{
"amount":{
"total":"7.47",
"currency":"USD"
}
}
]
}';
//open connection
$ch = curl_init();
//set connection properties
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
You can use json_decode to do the leg work for you.
Simply feed it the string that cURL passes back to you and it will in turn convert your JSON into a php usable array.