cURL PUT Request Not Working with PHP - php

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"
)
);

Related

PHP curl POST for Watson Video Enrichment

Am trying to use PHP 7.2 to submit a new job to the Watson Video Enrichment API.
Here's my code:
//set some vars for all tasks
$apiUrl = 'https://api-dal.watsonmedia.ibm.com/video-enrichment/v2';
$apiKey = 'xxxxxxxx';
//vars for this task
$path = '/jobs';
$name = 'Test1';
$notification_url = 'https://example.com/notification.php';
$url = 'https://example.com/video.mp4';
$data = array(
"name" => $name,
"notification_url" => $notification_url,
"preset" => "simple.custom-model",
"upload" => array(
"url" => $url
)
);
$data_string = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_URL, $apiUrl.$path );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string),
'Authorization: APIKey '.$apiKey
));
$result = curl_exec($ch);
echo $result;
But I can't get it to work, even with varying CURLOPTs, like:
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
I keep getting the response: Bad Request.
Here's the API docs.
Am I setting up the POST CURL all wrong? Is my $data array wrong? Any idea how to fix this?
Reading their API documentation, it looks like the field preset is not of type string. Rather it has a schema defined here. This appears similar to how you are using the upload schema.
You should change your data array to look like this:
$data = array(
"name" => $name,
"notification_url" => $notification_url,
"preset" => array(
"video_url" => "https://example.com/path/to/your/video"
),
"upload" => array(
"url" => $url
)
);
Ok, I figured it out. Thanks to #TheGentleman for pointing the way.
My data array should look like:
$data = array(
"name" => $name,
"notification_url" => $notification_url,
"preset" => array(
"simple.custom-model" => array(
"video_url" => $url,
"language" => "en-US"
)
),
"upload" => array(
"url" => $url
)
);

JIRA REST API POST call via cURL with OAuth access token returns 401

I have went through steps of getting an access token ( i did this though a terminal using java)
When i try to use a GET request in terminal with java like this:
java -jar rest-oauth-client-1.0.one-jar.jar request ACCESS_TOKEN http://locahost:8015/rest/api/2/issue/TP-113
It works and returns an Issue JSON
But when i try to GET or POST an ISSUE using PHP and cURL like this
$FieldSummary = substr($items[0]->summary,strpos($items[0]->summary,'- ')+2);
$FieldDescription = substr($items[0]->description,0);
$FieldStartTime = new DateTime($items[0]->start->dateTime);
$FieldEndTime = new DateTime($items[0]->end->dateTime);
$dateStart = $FieldStartTime->format("Y-m-d\Th:m:s.sOZ");
$dateEnd = $FieldEndTime->format("Y-m-d\Th:m:s.sOZ");
$url = "http://".$config['jiraAdress']."/rest/api/2/issue/".$FieldKey;
$ch = curl_init($url);
$headers = array(
'Authorization: Bearer ACCESS_TOKEN',
'Accept: application/json',
'Content-Type: application/json'
);
$data = json_encode ( array (
'fields' => array (
'summary' => $FieldSummary,
'description' => $FieldDescription,
'customfield_10007' => $dateStart,
'customfield_10008' => $dateEnd
)
), JSON_PRETTY_PRINT );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
It returns error 401 and error JSON:
{
"errorMessages": [
"You do not have the permission to see the specified issue.",
"Login Required"
],
"errors": {
}
}
What am I doing wrong? Remember that this is JIRA REST API.

Could not parse the 'params' argument as valid JSON (PHP, cURL, REST)

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.

Invalid Credentials error when trying to add post to blogger using API

I have trying to add a post to blogger using API since yesterday but no luck. Here is my code. When I run it, I get "Invalid Credentials" error.
I have also posted screenshot below which shows from where I am getting that key in Google Developer Console. May be I am picking up wrong key?
Code
<?php
$key = "J7c";
$blog_id = "123456";
$url = 'https://www.googleapis.com/blogger/v3/blogs/'.$blog_id.'/posts/';
$postData = array(
'kind' => 'blogger#post',
'blog' => array('id' => $blog_id),
'title' => 'This is title',
'content' => 'This is content'
);
$data_string = json_encode($postData);
$head = array();
$head[] = 'Authorization: '.$key;
$head[] = 'Content-Type: application/json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $head);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
$rsp = curl_exec($ch);
$results = json_decode($rsp);
var_dump($rsp);
?>
Error
Google Developer Console

Post object via CURL using PHP

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);

Categories