I am new to APIs and I am having trouble updating the status of issues. I have successfully listed projects and issues but not sure how to update them. I am using PHP to access the API. Can some one please share some example in php to how to update an issue using json. I have read the API documentation but its too minimal. I am not familiar with PUT and I cant find any good example or tutorial on the internet. Thankyou!
This what I tried but didnt work
function update_issue($issue_id){
// set url
$planio_url = "http://something.plan.io/issues/".issue_id.".json?key=185f1edsadfsdafy86578ce82ea70a0eb4267";
// create curl resource
$ch = curl_init();
$data = '{"issue":{"status":{"name":"done","id":21},"notes":"closing all old issues"}}';
$data = json_decode($data);
$ch = curl_init($planio_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
$response = curl_exec($ch);
if(!$response) {
return false;
}else{
return $response;
}
}
Returns empty response.
For everybody who wants to copy the snippet above. to update a ticket (like i did)
The URL in $planio_url is missing a $
..issues/".issue_id.".json
must be:
issues/".$issue_id.".json
also i decoded the json with true as second parameter to get an array for http_build_query
works like a charm.
Related
I am trying to connect to an API with Powershell using Invoke-RestMethod, but I keep running into an error for invalid credentials. I am converting the -Body to JSON in my API call. The documentation states there is no header and only 2 parameters: the username and password. I am also using -ContentType application/json, although I believe the issue lies with the JSON and not the Powershell itself.
The API has a test on their website where you can input your credentials to get an API key, which works fine so I know the credentials are correct. They also have a textbox to test authentication with JSON, which throws an error. The JSON I am testing looks like this:
{
"username": "myusername",
"password": "xxxxxxxx"
}
The API documentation also provides a sample PHP script to get an API key, which I confirmed does work when I put my credentials into it. The sample script is below:
$url = "https:website.com/api";
$content = ["username"=>"myusername","password"=>"********"];
$curl = curl_init($url);
if($curl) {
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// POST method
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($content));
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
$response = json_decode($json_response, true);
} else {
die("failed to connect\n");
}
I am not a PHP developer, but after looking up every command I believe I understand what it is doing for the most part. I am just wondering if I am missing anything in my JSON that the PHP script might add. In the PHP script I did convert the $content variable to JSON and it is identical to the JSON provided above, as well as the JSON used in the Powershell command.
As a side note, I have also tested it with with a SecureString in Powershell, which yielded the same results. Once I get the JSON to work I am planning on using a SecureString.
Any kind of assistance would be very helpful. Thank you!
I tried very hard to make output of Azuracast json query, but I cant, I'm a php guy and I don't quite get it. Tutorials didn't work either.
I got my token and eveyrthing. I just want to output a album name, song title and artist to string. That's it. I just want to make my little custom player, which is going well except for this. Can you guys check my code and help me out? I know this questions was already answered, but it didnt help for me. It always outputs nothing on my website.
my api link: here
//$url = "https://91.247.70.40/api/nowplaying/2.json";
//$data = file_get_contents($url);
//$infos = json_decode($data, true);
//$nazwa = $infos->cache;
//echo $nazwa;
function jwt_request($token, $post) {
$ch = curl_init('https://91.247.70.40/api/nowplaying/2');
header('Content-Type: application/json');
$post = json_encode($post);
$authorization = "Authorization: Bearer ".$token;
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization ));
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);
return json_decode($result, true);
}
echo $result->cache;
you can see i tried two ways. None worked. I've hidden the token. It is set up correctly, i know that.
You seem to be having multiple problems here. The first one is that your API is served on HTTPS but with an invalid certificate / self signed one. file_get_contents and curl will by default fail requests to pages with an untrusted cert. First you should setup a Let's Encrypt certificate as shown in the documentation.
Then your first code will correctly return the decoded JSON data. But you should remove the .json at the end of the URL since this is not a file you are requesting but an API route. There you try to access the decoded data as if it is an object but json_decode($data, true) returns an assoc array so you need to access the data like this $infos['cache']. If you want to get the album name, song title and artist from the data you need to access these fields: $infos['now_playing']['song']['album'], $infos['now_playing']['song']['title'] and $infos['now_playing']['song']['artist'].
Your second code example will not work after you fixed the certificate error because the /api/nowplaying/{station_id} route is a GET route but you define the CURL request as a POST request.
I am working with rest API. I am using zoho API's for making calls. Working in yii2 I am trying to call a GET API which gives me some details about my project.
/* Set the Request Url (without Parameters) here */
$request_url = 'https://projectsapi.zoho.com/restapi/portal/[PORTALID]/projects/[PROJECTID]/bugs/defaultfields/';
$ch = curl_init($request_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('authtoken' => 'key')); // here i am using key but in actual i am putting the real key value
$curl_response = curl_exec($ch);
$json = json_decode($curl_response);
var_dump($json);
exit();
Accessing it via POSTMAN when I run this call it gives me NULL in response. I have also tried out the way mentioned in the PHP Example here. But it doesn't work out for me as well.
I must be missing something that I don't know.
Any help would be highly appreciated.
Try checking your headers and make sure you're passing through all the required fields for an example authorization, content type etc.
So have this laravel get product api that works if i called it from the browser as you can see from this image
here is the curl code chunk that tried to get this json response
$url = 'http://localhost:8000/api/products';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
$result = json_decode($data,true);
echo $result;
but i cannot get anything in result, nothing to echo. I have one mockable api in https://demo0546443.mockable.io/products/getAllProducts that produce nearly the same json as my localhost api and it return response as it should have. please help
SOLUTION
So after countless hours researching about this, it seems laravel needs to be accessed on static ip, not on localhost.
It’s recommended to use a library like Guzzle though to make HTTP requests instead though.its very eazy to implement and work with
Rather than echo $result try return response()->json($data).
echo $result return Header application/html while json() return application/json hope this helps.
New Twilio developer here. My app uses the IBM Watson Speech-to-text Add-on, but I'm having trouble accessing the results payload in my callback. I can't find helpful documentation or any discussion of the issue in forums.
What I know/What I've tried
The payload resource exists – I'm able to access it directly via browser.
Using the syntax prescribed by the Twilio PHP helper library client returns a 61005 "Bad request" error:
$request = $client->v1->lookups
->phoneNumbers("+1XXXXXXXXXX")
->fetch(
array(
"AddOns" => "ibm_watson_speechtotext",
));
Using cURL to get the resource directly has been equally unfruitful, returning an empty string.
$request = json_decode($_REQUEST['AddOns']);
error_log("URL: ".$request->results->ibm_watson_speechtotext->payload[0]->url);
$ch = curl_init($request->results->ibm_watson_speechtotext->payload[0]->url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$account_sid:$token");
$json = curl_exec($ch);
if($json === false) {
error_log("cURL error: ".curl_error($ch));
error_log(print_r($json,true));
}
curl_close($ch);
$obj = json_decode($json);
Any recommendations?
The following resources should help you find the results you're looking for.
Your first code snippet above doesn't apply (Lookup is a different product).
instead you will want to use the add-on results api to grab the results.
https://www.twilio.com/docs/api/add-ons/results-api
For your second snippet, you will need to enable follow redirect option with CURL.
Clients will need to follow the redirect to receive the data
associated with a Payload resource.
These may also help as you explore add-ons:
https://www.twilio.com/docs/api/add-ons/using-add-ons#add-on-results-available-callback
and
https://www.twilio.com/docs/guides/voice/how-to-use-recordings-add-ons-in-python