redmine rest api post method with php - php

i'm trying to add a new user to redmine using REST api :
<?php
$service_url = "https://pmt.proxym-it.tn?key=9ed507d365eaa598a0f134c3eeb3efa062eb72de&method=POST&format=json";
$curl = curl_init($service_url);
$curl_post_data = array('user' => array("lastname"=>'Lachtar',"firstname"=>'Abdelfetteh',"login"=>'fetteh',"mail"=>'abdelfett ehlachtar#yahoo.fr',"password"=>'secret'));
$curl_post_data = json_encode($curl_post_data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$curl_response = curl_exec($curl);
if ($curl_response === false) {
$info = curl_getinfo($curl);
curl_close($curl);
die('error occured during curl exec. Additioanl info: ' . var_export($info));
}
curl_close($curl);
$decoded = json_decode($curl_response);
if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
die('error occured: ' . $decoded->response->errormessage);
}
echo 'response ok!';
var_export($decoded);
?>
but the response returned is null.

when using
http://username:password#localhost:9000
as url, its showing same error response ok!NULL but in redmine log its showing the following
Parameters: {"{\"user\":{\"lastname\":\"test\",\"firstname\":\"rest\",\"login\":\"rest\",\"mail\":\"rest#test.com\",\"password\":\"12345678\"}}"=>"[FILTERED]", "method"=>"POST"}
Current user: anonymous
Filter chain halted as :check_if_login_required rendered or redirected
Completed 401 Unauthorized in 3.3ms (ActiveRecord: 0.3ms)

Related

UPDATE records using REST api

I am working on PHP and Salesforce integration and I want to update records using PHP.
I want to update records in Salesforce when clicking on the Button "Follow up".
And also try the below code.
function update_account($id, $followup, $instance_url, $access_token) {
$url = "$instance_url/services/data/v20.0/sobjects/Contact/$id";
$content = json_encode(array("shivendra__FollowUp__c" => $followup));
echo $content;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER,
array("Authorization: OAuth $access_token",
"Content-type: application/json"));
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PATCH");
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 204 ) {
die("Error: call to URL $url failed with status $status, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
}
echo "<b>update_contact function</b>";
echo "HTTP status $status updating Contact<br/><br/>";
curl_close($curl);
}
But getting an error:
{"shivendra__FollowUp__c":null}
[{"errorCode":"METHOD_NOT_ALLOWED","message":"HTTP Method 'PATCH' not allowed. Allowed are HEAD,GET,POST"}]
Error: call to URL https://shivendra-dev-ed.my.salesforce.com/services/data/v20.0/sobjects/Contact/ failed with status 405, curl_error , curl_errno 0
Try the post method with cURL, similar to this:
curl_setopt($ch, CURLOPT_POST, 1);

PHP CURL having 400 while it works

While checking the URL: https://i2.wp.com/jarek-kefir.org/wp-content/uploads/2019/04/pożar-katedry-notre-dame.jpg?ssl=1
I have result status code 400...
This is my function:
public function callAPI($url)
{
$curl = curl_init();
error_log('Checking url: ' . $url);
// OPTIONS:
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
//'APIKEY: 111111111111111111111',
'Content-Type: application/json',
));
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
// EXECUTE:
$result = utf8_decode(curl_exec($curl));
$result = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $result);
if (!$result) {
error_log('Connection Failure ' . $result . ' for url: ' . $url);
}
// Check if any error occurred
$error_msg = '';
$error_number = curl_errno($curl);
$error_info = curl_error($curl);
$info = curl_getinfo($curl);
$httpCode = $info['http_code'];
$request_ok = $httpCode == 200 || $httpCode == 201 || $httpCode == 204;
error_log('$httpCode: ' . $httpCode);
if (!$request_ok) {
$info = curl_error($curl);
$error_msg = $info ? $info : "Http code: " . $httpCode;
error_log('Error while checking url: ' . $url . ' : ' . $error_msg);
}
curl_close($curl);
return $error_msg;
}
but when I check it manually or even by curl from command line it works fine.
Probably I miss some configuration for curl here..
heh finally I found out that it is because one polish character.. :)

sending a response to another php file using $curl returns an error

I am trying to send JSON data to another php file for now for learning purpose (actually meant to be sent to a react-native app) but this returns an error saying
Error: call to URL http://localhost/hhh.php failed with status 200, response , curl_error , curl_errno 0
if($cliSeq==0)
{
$welcomearr= array("welcome");
$url = "http://localhost/hhh.php";
$welcomeJSON = json_encode($welcomearr);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER,
array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $welcomeJSON);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 201 ) {
die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
}
curl_close($curl);
$response = json_decode($json_response, true);
}

Wowza Cloud API: curl requests

I can't make it work.. :( I have this function (for create passthrough transcoder), when I run I see NULL in the web. If I test directly from the browser with the url, it does notify me that there is a problem an auth (apikey and acceskey)
function createPassthrough($name, $source_url, $recording = null)
{
$url = "https://sandbox.cloud.wowza.com/api/v1/transcoders";
$json = '{
"transcoder":{
"billing_mode":"pay_as_you_go",
"broadcast_location":"eu_belgium",
"delivery_method":"pull",
"name":"prueba",
"protocol":"rtsp",
"source_url":"url_camara",
"transcoder_Type":"passthrough",
"low_latency":true,
"buffer_size":0,
"play_maximum_connections":100,
"stream_smoother":false
}
}';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept:application/json; charset=utf-8',
'Content-Type: application/json; charset=utf-8',
'wsc-api-key:' . $apiKey,
'wsc-access-key:' . $accessKey,
));
$result = curl_exec($ch);
curl_close($ch);
$obj = json_decode($result);
var_dump($obj);
}
What am I doing wrong? Thanks in advance.
You should check for curl errors after $result = curl_exec($ch);.
// Check for errors and display the error message
if($errno = curl_errno($ch)) {
$error_message = curl_strerror($errno);
echo "cURL error ({$errno}):\n {$error_message}";
}

REST API service: how to call it correctly via PHP curl?

Let's say I have a running service API that responds to a url like:
http://example.com/param/1
It's served on both get and post verbs.
Param=1 is the param considered
How should I perform correctly a curl request to pass the parameter (POST)?
If i browse: http://example.com/param/1
I'll have back the expected response (GET)
But if I do the following, it is not going to work:
$service_url = 'http://example.com/';
$curl = curl_init($service_url);
$curl_post_data = array('param'=>1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
$curl_response = curl_exec($curl);
if ($curl_response === false) {
$info = curl_getinfo($curl);
curl_close($curl);
die('error occured during curl exec. Additional info: ' . var_export($info));
}
curl_close($curl);
$decoded = json_decode($curl_response);
if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
die('error occured: ' . $decoded->response->errormessage);
}
echo 'response ok!';
var_export($decoded->response);
Even changing it like the folowing won't work:
$service_url = 'http://example.com/param/1';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
$curl_response = curl_exec($curl);
if ($curl_response === false) {
$info = curl_getinfo($curl);
curl_close($curl);
die('error occured during curl exec. Additional info: ' . var_export($info));
}
curl_close($curl);
$decoded = json_decode($curl_response);
if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
die('error occured: ' . $decoded->response->errormessage);
}
echo 'response ok!';
var_export($decoded->response);
This instead works: but i guess it still will be a GET
$service_url = 'http://example.com/param/1';
$curl = curl_init($service_url);
$curl_response = curl_exec($curl);
if ($curl_response === false) {
$info = curl_getinfo($curl);
curl_close($curl);
die('error occured during curl exec. Additional info: ' . var_export($info));
}
curl_close($curl);
$decoded = json_decode($curl_response);
if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
die('error occured: ' . $decoded->response->errormessage);
}
echo 'response ok!';
var_export($decoded->response);
$service_url = 'http://example.com/param/1';
$ch = curl_init($service_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "param1=value1&param2=value2&..");
$response = curl_exec($ch);
curl_close($ch);

Categories