0

I need to execute some code present inside one PHP file and this file will invoked from other PHP file using CURL but simply as per my code its not happening like that.

I am explaining my code below.

test.php:

<?php
include('db.php');
$adminUrl1='http://example.com/api/product/test1.php';
$data1 = array("user_id" => 54, "org_ord_id" => 82);
$data_string1 = json_encode($data1);                       
$ch1 = curl_init($adminUrl1); 
curl_setopt($ch1, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch1, CURLOPT_POSTFIELDS, $data_string1);                                                                  
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch1, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string1))                                                                       
);
$res = curl_exec($ch1);
$array = json_decode($res, 1);
echo($array);exit;
?>

Here I am trying to execute the test1.php file which is present inside api/product/ directory.

test1.php:

include('db.php');
$user_id=$_POST['user_id'];
$order_id=$_POST['org_ord_id'];
echo $user_id.'::::'.$order_id;

Here I need to print those post param value those are passed from test.php but as per my current code its not printing anything.

subhra
  • 173
  • 5

1 Answers1

0

Remove this 'Content-Type: application/json', because you need to get values like application/x-www-form-urlencoded for $_POST[...].

And put application/json to CURLOPT_HEADERFUNCTION for response.

CURL_OPT DOC

daremachine
  • 2,678
  • 2
  • 23
  • 34