Having trouble making this PHP curl put request to work - php

I'm trying to make a put request to my server, but, nothing is happening when I call it. Here's what I have:
$url = ("http://my.server/api/");
$data = array("{\"bri\": 254, \"on\": true,\"bri\": 10}" => $a);
$ch = curl_init($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;
}

Related

PHP curl request reading data

I want to send a data using curl and return some data
I'am using the following code
$data = array('type' => 'active');
$result = '';
$url = 'https://example.com/'.$function_name;
$send_data = json_encode($data, TRUE);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 80);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $send_data);
$result = curl_exec($ch);
curl_close($ch);
print_r($result);exit;
On the receiving end returning the POST data
print_r($_POST);exit;
But I'am getting data in different structure as below
(
[{"type":"active"}] =>
)
I think due to this $_POST['type'] is not getting in the receiving side

How to send data to another page via php

I want to send some data to another server but I don't want user to view the data.
I tried submitting form but others still manage to see the data even it is hidden.
PS: The server only receives $_POST.
You use curl for this purpose.
An example:
<?PHP
function POST($url,$data,$headers,$type)
{
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt ($ch, CURLOPT_POST, 1);
if($type === 1)
{
curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($data));
}
else
{
curl_setopt($ch,CURLOPT_POSTFIELDS, $data);
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$output = curl_exec ($ch);
$posturl = curl_getinfo($ch,CURLINFO_EFFECTIVE_URL);
$httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
$cURLinfo = curl_getinfo($ch);
curl_close($ch);
return array(
"output" => $output,
"posturl" => $posturl,
"httpcode" => $httpCode,
"diagnostics" => $cURLinfo
);
}
?>
But most likely, this is a duplicate of How do I send a POST request with PHP?
use forge.js to encrypt the data in base64 e.g.
jQuery.post('yourfile.php',
{uname: forge.util.encode64(jQuery('#uname').val()) ,
upass: forge.util.encode64(jQuery('#upass').val() )
},function(data)
{
console.log(data);
});
and then in PHP you can use
$user = base64_decode($_POST['uname']);
$passw = base64_decode($_POST['upass']);
to retrieve the data.

Can't hit Web API from php

I'm trying to call this
[Route("CreateHL7TestFile")]
[HttpPost]
public IActionResult CreateHL7TestFile([FromBody] JObject data)
{
// Code here
}
from a php function using cUrl. When I try and call the API without passing any data it works fine. It's when I try and add the '$data' that I cannot hit the API. When I call the API with data from Postman it also works fine. So I'm assuming my error is on the php side?
My php function looks like this.
public function CallAPI($method, $url, $data = false)
{
$data = array("value1" => "some value");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('test1' => 'value1', 'test2' => 'value2')));
$result = curl_exec($ch);
print_r($result);
curl_close($ch);
}
http://localhost:50999/api/ParticipantAPI/CreateHL7TestFile
Any help would be much appreciated.
Thanks,
Adam
Use a proper model on the asp.net-core server to bind incoming data
public class MyData {
public string value1 { get; set;}
}
And update action accordingly
[Route("CreateHL7TestFile")]
[HttpPost]
public IActionResult CreateHL7TestFile([FromBody] MyData data) {
// Code here
}
Referencing the following article POSTing JSON Data With PHP cURL
Try the following on the PHP side
$data = array("value1" => "some value");
$data_string = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
print_r($result);
curl_close($ch);
For your real requests refactor the expected data model accordingly.
Try something like this:
public function CallAPI($method, $url, $data = false)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('test1' => 'value1', 'test2' => 'value2')));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
print_r($result);
curl_close($ch);
}
http://php.net/manual/en/function.http-build-query.php
If you want to send json, then I think you'll have to set the header, something like:
public function CallAPI($method, $url, $data = false)
{
$dat = array("TEST1","TEST2");
$data = json_encode($dat);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
print_r($result);
curl_close($ch);
}

PHP curl get multidimensional array

I'm trying to get a list of issues using gitlab API. The response is correct testing the URL, but the curl doesnot execute in my php code. I guess it`s because of the multidimensional array. but how do you solve this? Here is my function:
function getissues_list () {
$url = MAIN_URL."/issues";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPGET , true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->token);
$response = curl_exec($ch);
if (!curl_exec($ch)) {
echo ($this->exception_message);
} else{
return $response;
curl_close($ch);
}
}
Thanks!
function getissues_list () {
global MAIN_URL; // Is this accessible in this function?
$url = MAIN_URL."/issues";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPGET , true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->token);
$response = curl_exec($ch);
var_dump($response);// ADD THIS LINE TO DEBUG!!!!!!!!
if (!curl_exec($ch)) {
echo ($this->exception_message);
} else{
return $response;
curl_close($ch);
}
}

how to make one global function in code igniter?

I am using rest api's in my website..for that i have to make curl requests in every controller many times like that....
$data = array('id' => '01','user_name' => 'abc');
$data_json = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://abc.xyz.com/1.0/index.php/abc_ctrl/function");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
$output = curl_exec($ch);
curl_close($ch);
$output3 = json_decode($output);
functionality of every curl is same, like it makes a post call by sending a post data to rest controller.....
i am tired of using same lines every time everyware...I want to make a post request function in config file or i dont know in main file, which accept parameter as url and post data and returns output...
so everytime i have to call that function in config and i got results...
You may do it like this:
public function FunctionName()
{
$data = array('id' => '01','user_name' => 'abc');
$url = "http://abc.xyz.com/1.0/index.php/abc_ctrl/function";
$result = $this->CurlAPI($data,$url);
}
public function CurlAPI($data,$url)
{
$data_json = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
$output = curl_exec($ch);
curl_close($ch);
$output3 = json_decode($output);
return $output3;
}
UPDATE:
You may also write this function in helper file as:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
public function CurlAPI($data,$url)
{
$data_json = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
$output = curl_exec($ch);
curl_close($ch);
$output3 = json_decode($output);
return $output3;
}
Save this to application/helpers/ . We shall call it "new_helper.php"
This can be in your controller, model or view (not preferable)
$this->load->helper('new_helper');
echo CurlAPI($data,$url);

Categories