0

i have this code (works just fine in localhost)

$post_data = array( 'username' => $username, 'password' => $password );
$opts = array( 'http' => array(
        'method' => 'POST',
        'header' => 'Content-type: application/x-www-form-urlencoded',
        'content' => http_build_query( $post_data ) ) );

$context = stream_context_create( $opts );

$api_result = json_decode( file_get_contents( $panel_url . "api.php?action=user&sub=info", false, $context ), true );

var_dump ($api_result);

but on my server file_get_contents not works, i tried to use curl but i dont know exactly how to change

$post_data = array( 'username' => $username, 'password' => $password );
$opts = array( 'http' => array(
        'method' => 'POST',
        'header' => 'Content-type: application/x-www-form-urlencoded',
        'content' => http_build_query( $post_data ) ) );

$context = stream_context_create( $opts );


$runfile = $panel_url . "api.php?action=user&sub=info";

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $runfile);

    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

    $content = curl_exec ($ch);

    curl_close ($ch); 


     $api_result = json_decode( $content );

i changed the code to this, works in localhost but on my server i get bool(false)

$post_data = array('action'=>'user','sub'=>'info','username' => $username, 'password' => $password );
$opts = array( 'http' => array(
        'method' => 'POST',
        'header' => 'Content-type: application/x-www-form-urlencoded',
        'content' => http_build_query( $post_data ) ) );

$context = stream_context_create( $opts );

//$api_result = json_decode( file_get_contents( $panel_url . "api.php?action=user&sub=info", false, $context ), true );

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$panel_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);


var_dump ($server_output);
Alvaro Louzada
  • 433
  • 1
  • 6
  • 23

0 Answers0