Good evening, I have an account on Todoist. I would use the Todoist API to get all the projects. I wrote the following code:
$url = "https://todoist.com/API/v6/sync";
$post_data = array(
'token' => "12345678901234567890abcdefabcdef01234567",
'seq_no' => "0",
'resource_types' => '["projects"]'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$output = curl_exec($ch);
print_r($output);
curl_close($ch);
The output is a string like this:
{"TempIdMapping":{},"seq_no_global":6201059540,"seq_no":6201059540,"UserId":7179424,"Projects":[{"user_id":7179424,"name":"Project1","color":1,"is_deleted":0,"collapsed":0,"id":165361294,"archived_date":null,"item_order":1,"indent":1,"archived_timestamp":0,"shared":false,"is_archived":0},{"indent":1,"name":"Inbox","user_id":7179424,"color":7,"is_deleted":0,"collapsed":0,"inbox_project":true,"archived_date":null,"item_order":0,"is_archived":0,"archived_timestamp":0,"shared":false,"id":165339673}]}
Is there a way to convert this output into an array?
Example:
TempIdMapping => {},
seq_no_global => 6201059540,
seq_no => 6201059540
and so on...
Your output is JSON formated, so you can use json_decode like so
json_decode($output, true);
The second parameter convert the result to an associative array instead of an stdObject
Related
I am working with the Challonge.com API found here: https://api.challonge.com/v1
I am trying to get the match UPDATE feature to work - https://api.challonge.com/v1/documents/matches/update
I have had success updating my tournament with the same code, but for some reason the following code is not updating the variables. Instead the response I get is the same as before the script is ran. Any Ideas?
// Update Match on Challonge
$params = array(
"api_key" => "my api key goes here",
"winner_id" => "50287554",
"scores_csv" => "2-5,1-3"
);
$url = "https://api.challonge.com/v1/tournaments/efps_59/matches/78842711.json";
$data_json = json_encode($params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data_json)));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
Your documentation states that the parameters for winner_id and scores_csvhave to be an array of match:
// Update Match on Challonge
$params = array(
"api_key" => "my api key goes here",
"match" => array(
"winner_id" => "50287554",
"scores_csv" => "2-5,1-3"
)
);
I want to send json data using cURL in php, but the problem is that cURL is not posting any data.
NOTICE: cURL is properly installed and configured.
$ch = curl_init($url);
//The JSON data.
$jsonData = '{
"recipient":{
"id":"'.$sender.'"
},
"message":{
"text":"'.$message_to_reply.'"
}
}';
$jsonDataEncoded = $jsonData;
//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, array($jsonDataEncoded));
//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_exec($ch);
The json Data is working fine but the cURL post is not posting anything and also not giving any type of warnings/notice or error.
as far as i can see, you do 3 mistakes
1: don't do curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");, the correct way to tell curl that you want a POST request is curl_setopt($ch, CURLOPT_POST, true);
2: when you give CURLOPT_POSTFIELDS an array, its actually converted to a multipart/form-data encoding, which is not what you want (you want to transfer a json)
3: your $sender and $message_to_reply seem to be just inserted in to the json raw. what happens if your $message_to_reply contains an " or ' ? it will invalidate the json. consider encoding it properly, for example using json_encode, like
$jsonData = array (
'recipient' => array (
'id' => $sender
),
'message' => array (
'text' => $messaage_to_reply
)
);
$jsonDataEncoded = json_encode ( $jsonData, JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
but, provided that $sender and $message_to_reply is already properly json encoded, the only reason your original code doesn't work, as far as i can see, is that you give CURLOPT_POSTFIELDS an array, thus, all that's needed to fix it would be to remove "array" from that line, like curl_setopt($ch, CURLOPT_POSTFIELDS,$jsonDataEncoded);
Try this;
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(json_decode($jsonDataEncoded)));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
You probably don't want to pass all data to one key.
Output of print_r(array($jsonDataEncoded))
Array ( [0] => { "recipient":{ "id":"me" }, "message":{ "text":"hello" } } )
Output of print_r(json_decode(array($jsonDataEncoded)))
Array ( [0] => stdClass Object ( [recipient] => stdClass Object ( [id] => me ) [message] => stdClass Object ( [text] => hello ) ) )
Well after all the try, Here is the answer:
$jsonData = '{
"recipient":{
"id":"'.$sender.'"
},
"message":{
"text":"'.$message_to_reply.'"
}
}';
$jsonDataEncoded = $jsonData;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
//Here i removed the array//
//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// By default in PHP7 CURL_SSL_VERIFYPEER, is true. You have to make it false//
$result = curl_exec($ch);
After executing the PHP Script, I am getting the following error – Any help is appreciated. Based on my understanding, I created an array based on the bugzilla documentation - Thanks in advance
https://bugzilla.readthedocs.io/en/5.0/api/core/v1/bug.html#create-bug
{"code":32000,"error":true,"message":"Could not parse the 'params' argument as valid JSON. Error: malformed number (no digits after initial minus), at character offset 1
$url ="http://localhost:8080/bugzilla/rest/bug";
$data = array(
"product" => "TestProduct",
"component" => "TestComponent",
"version" => "unspecified",
"summary" => "'This is a test bug - please disregard",
"alias" => "SomeAlias",
"op_sys" => "All",
"priority" => "P1",
"rep_platform" => "All"
);
$str_data = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array("Content-Type: application/json", "Accept: application/json"));
$username = 'ashish.sureka#in.abb.com';
$password = 'incrc';
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
$result = curl_exec($ch);
curl_close($ch);
echo $result
You have this:
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
which sends your array of data as a conventional key=value form submission, when you should have
curl_setopt($ch, CURLOPT_POSTFIELDS, $str_data);
^^^^^
which would send your JSON-encoded array instead.
You send $data in CURLOPT_POSTFIELDS but I think you need to give it $str_data which is encode in JSON
I think you have to replace
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
with
curl_setopt($ch, CURLOPT_POSTFIELDS, $str_data);
to transfer the json encoded array instead of the raw php array.
I am having trouble converting an array into xml and then posting it to an third party URL as an xml post.
I believe i am close but i am missing something for it to work.
I am using wordpress and gravity form (I don't think it matters)
Here is what i have up-till now.
function post_to_third_party($entry, $form) {
$post_url = 'https://xxxx.com/home/BorrowerImport.do?CampaignID=xxx';
$body = array( 'firstname' => $entry['8.3'],
'lastname' => $entry['8.6'],
'dayphone' => $entry['12'],
'email' => $entry['11']
);
$xml = new SimpleXMLElement('<application/>');
$body = array_flip($body);
array_walk($body, array ($xml, 'addChild'));
print $xml->asXML();
$ch = curl_init($url);
//curl_setopt($ch, CURLOPT_MUTE, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$output = curl_exec($ch);
echo $output;
curl_close($ch);
}
I also tried the following code this seems to work but the var_dump looks like string(201) " $firstname $lastname $dayphone $email " ok
I don't know how to populate the xml tags with the data gathered from the $body array
Here is the code i used for this result
add_action('gform_after_submission', 'post_to_third_party', 10, 2);
function post_to_third_party($entry, $form) {
$post_url = 'https://xxxx.com/home/BorrowerImport.do?CampaignID=xxx';
$body = array(
'firstname' => $entry['8.3'],
'lastname' => $entry['8.6'],
'dayphone' => $entry['12'],
'email' => $entry['11']
);
$xml = '
<?xml version="1.0" encoding="UTF-8"?>
<application>
<firstname>$firstname</firstname>
<lastname>$lastname</lastname>
<dayphone>$dayphone</dayphone>
<email>$email</email>
</application>';
var_dump($xml);
$ch = curl_init($url);
//curl_setopt($ch, CURLOPT_MUTE, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$output = curl_exec($ch);
echo $output;
curl_close($ch);
}
Ok, here's the problem:
The method you are using to build the array before converting to XML is accidentally deleting some of the data.
You start out with something like this:
$body = array(
"firstname" => "Test",
"lastname" => "Test",
"dayphone" => "(801)735-2222",
"email" => "test#gmail.com",
);
Then you call array_flip, which gives you this:
$body = array(
"Test" => "firstname",
"Test" => "lastname",
"(801)735-2222" => "dayphone",
"test#gmail.com" => "email",
);
You now have two "Test" keys in your array. PHP keeps the last one an throws away the first. So your XML document is now missing one of the required keys: "firstname".
The solution is to build your XML document the old fashioned way and stay away from fancy stuff like array_flip:
foreach($body as $k=$v)
$xml->addChild($k, $v);
We are attempting to post to a rest API endpoint that requires a "POST object" within one of the keys. In javascript/jquery, this works fine. But, using CURL in PHP the endpoint doesn't receive the object (called "components") here:
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_URL, 'http://api.sitesitesite.com');
$comps = array('slug' => "xyz",
'visble' => 1,
'color' => "xyz",
'shape' => "xyz",
'version' => "2",
);
$post_args = array();
$post_args['components'] = $comps;
$post_args['id'] = $id;
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_args);
$result = curl_exec($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
Get rid of "CURLOPT_CUSTOMREQUEST" and use "CURLOPT_POST"
curl_setopt($ch, CURLOPT_POST, true);