how to make one global function in code igniter? - php

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

Related

Parameter not passing in curl using Php

I am trying to fetch data from webservice(nodejs) using curl in php but i am getting result "lang is required"
i tried with following code but not working for me where i am wrong ?
Here is my code
$post = ['lang'=> "593f973dea53161779dd5660",'password'=> "amit123d"];
$ch = curl_init();
$url="http://xxxxxx:8000/api/employer/login";
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$response = curl_exec($ch);
$result = json_decode($response);
echo "<pre>";print_R($result);
Usually to send post variables i use : the http_build_query function.
$url = "http://xxxxxx:8000/api/employer/login";
$post = ['lang'=> "593f973dea53161779dd5660",'password'=> "amit123d"];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
$response = curl_exec($ch);
$result = json_decode($response);
echo "<pre>";print_R($result);
It works better.
first check the request whether send post field or not.
$post = ['lang'=> "593f973dea53161779dd5660",'password'=> "amit123d"];
$ch = curl_init();
$url="https://httpbin.org/post";
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$response = curl_exec($ch);
$result = json_decode($response);
echo "<pre>";print_R($result);
the site: https://httpbin.org/ will response the post field if the post data exists.
I think the problem may occurs the nodejs side. Does it check the post field rightly ?
from my experience your code is right.

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

Having trouble making this PHP curl put request to work

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

Post Curl issue

<?php
$url = "http://website.com/folder/index.php";
$data = array('id' => 'R98s', 'name' => 'Bob', 'content' => 'Hello');
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
curl_exec($handle);
?>
This works great, only 1 problem though,
id like a way to get the content response from the posted data in a variable, and not show as if its the page.
Try the following:
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE ); // return into a variable
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$result = curl_exec( $ch ); // run!
curl_close($ch);
And never forget the curl_close($handle); at the end.
I always thought you needed this too
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($handle)

Categories