Why curl to another server dont work in codeigniter? - codeigniter

I am working on codeigniter and I am doing a POST request curl for another project that is located on another server that is on another network.
This is my code.
controller from my webpage
function submit_form() {
error_log("data: ". $_POST);
$data = $this->input->post('data');
$data = json_decode($data,true);
error_log("data id: ".$data['idParceiro']);
// where are we posting to?
$url = 'http://mywebdomain/ncsync/Sync/getLogin';
$fields = array('idParceiro' => $data['idParceiro']);
// build the urlencoded data
$postvars = http_build_query($fields);
// open connection
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);
// execute post
$result = curl_exec($ch);
The error_log on the server that receives the post is not being printed, hence I conclude that nothing is coming.
this is my .htaccess in my server that receive the request in folder /application
<IfModule authz_core_module>
Require all denied
</IfModule>
<IfModule !authz_core_module>
Deny from all
</IfModule>
Note: This works on localhost when I am on the same network as the server to which I am requesting.
Can help me?

Related

curl with laravel return session expired

I use curl with laravel and I need to launch a function inside a controller.
but when I put
dd('test');
after the namespace it's print.but when I try with the function a have a session expired
this is the code for the curl
// Préparation des données
$data = [];
$data["code__client"] = "VERTDIS13";
$data["status"] = "90";
$data["numero__incident"] = "INC544842";
// On tranforme le tableau PHP en objet JSON
$data = json_encode($data);
// Envoi au serveur
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://localhost:3000/notification/server");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "data=" . $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, true);
$server_output = curl_exec($ch);
$error = curl_error($ch);
if($error!=""){ dd($error);}
curl_close($ch);
// Ici pour tester j'affiche ce que le serveur renvoi
return $server_output;
inside the controller I tried to print data but it doesn't work
do you see the issue ?
thanks for help .
The session expired response sounds to me as if you are performing multiple consecutive curl requests to your application. If that's the case and your application depends on session cookies you can create a simple text file that is read and writeable to the script that does the curl requests, and add this to your code:
$cookie_file = "/the/path/to/your/cookiefile.txt";
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
Change the path in $cookie_file to the full filesystem path of the text file you create and curl will persist session cookies that are being returned with responses and use them in consecutive requests.
For this to work I think you should remove the line
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
because this would advise curl to ignore the cookies in the file.
Besides that I think the line
curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, true);
is also obsolete, because you don't provide basic auth credentials in your curl request.
I presume you extract data from within the application?
To me, it looks like CSRF token is missing or is in fact invalid.
for ($i = 0; $i < $metas->length; $i++){
$meta = $metas->item($i);
if($meta->getAttribute('name') == 'csrf-token')
$csrfToken = $meta->getAttribute('content');
}
$headers = array();
$headers[] = "Cookie: X-CSRF-Token=$csrfToken";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
More about CSRF & cURL
Give this a go

POST requests require a Content-length header in laravel

$output = Curl::httpGet("https://bhleh.com/emailstorage", "POST", $params);
return $output;
this is in my laravel but when ever i try to run this i get an error saying
411. That’s an error.
POST requests require a Content-length header. That’s all we know.
i tried adding header files in my middle ware folder but nothing seems to work.
i figured out that the Content-length header isn't included in laravel (i think so not so sure) so how would i add it
please note i am very new to laravel
this fix to this is that this i replaced the whole code with this
$data_string = json_encode($params);
$ch = curl_init('https://bhleh.com/emailstorage');
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);
return $result;
I was facing the same issue but I was not sending CURLOPT_POSTFIELDS as per the API endpoint. I don't need to send any variable but when I send CURLOPT_POSTFIELDS with a blank array it started working.
$data_string = array();
$ch = curl_init('https://bhleh.com/emailstorage');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data_string));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
);
$result = curl_exec($ch);
return $result;

Codeigniter hit an url and get back

I am using codeigniter 3.0 in my webapp. I want to hit an URL then get back to a view. Lets say on clicking a button, I want to hit an API then return back to the same view with success message. Can anyone help?
My API is http://wmlab.in/api/getData.php?username=web
I tried file('http://wmlab.in/api/getData.php?username=web') but it is not working.
use curl to do that and also install curl if your server did not have curl.
url="http://wmlab.in/api/getData.php?username=web";
function getUrlContent($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$data = curl_exec($ch);
echo $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return ($httpcode>=200 && $httpcode<300) ? $data : false;
}
$content=getUrlContent($url);
echo $content;
You can use file_get_contents('http://wmlab.in/api/getData.php?username=web') or can use curl;
For Curl use below code:-
$channel = curl_init();
curl_setopt( $channel, CURLOPT_URL, "http://wmlab.in/api/getData.php?username=web" );
curl_setopt( $channel, CURLOPT_RETURNTRANSFER, 1 );
$response= curl_exec ( $channel );
curl_close ( $channel );
// after curl response redirect to othe page
redirect('anypage');
For file get content use below code:-
$response=file_get_contents('http://wmlab.in/api/getData.php?username=web')
// after response redirect to othe page
redirect('anypage');
Hope it will help you.
it should be work
$result=file_get_contents('http://wmlab.in/api/getData.php?username=web');

500 Internal Server Error on Payza IPN Handler

I am migrating an app from PayPal to Payza (formerly AlertPay). When I run the code directly from the browser to test the token handling it shows up in the logs and I get a proper response: token=47TXhMVgdPrmV3n5aauIZ5CC0MrytYXWjID81pjVnQsEhkSUklPXT3clXZ4SFUFOL5WqepRUpBz5SKomoyPuDw==& INVALID TOKEN. When I run the full IPN response directly in the browser I get a fail but no visible errors.
But when I generate a transaction or resend an IPN from Payza it is not showing up in the log and I am getting a 500 Internal Server Error. I have turned on allow Query Strings in the CI config file.
class PayzaIPN {
function validate_ipn($payzaIPN ='',$logId = null) {
define("IPN_V2_HANDLER", "https://secure.payza.com/ipn2.ashx");
define("TOKEN_IDENTIFIER", "token=");
$_POST['token'] = '';
$token = urlencode($_POST['token']);
$token = TOKEN_IDENTIFIER.$payzaIPN;
$response = '';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, IPN_V2_HANDLER);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $token);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);
if(strlen($response) > 0)

send post include text and image by xmlrpc to wordpress

Im looking for a way to send remotely content(text and image ) by xmlrpc to wordpress but i couldn't find yet, im going to sent remotely content (text and image) over 100 post per minute to wordpress, whats the best way to do it? thanks so much
Use XMLRPC. Something like this:
$params = array(0,$username,$password,$content,true);
$request = xmlrpc_encode_request('metaWeblog.newPost',$params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($ch, CURLOPT_URL, $rpcurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
$results = curl_exec($ch);
curl_close($ch);

Resources