-1

I am using codeigniter framework.

I want to retrieve the data from the URL provided. I already tried this answers: tried.

Problem is that when i access the url that time it is printing the data. but when i try it with file_get_contents function, it is not going to print any data.

<?php
$url ='https://test.com/getSessionData';
$test = file_get_contents($url);
$t = json_decode($test);
var_dump($t);
?>

That url returns json data like:

{
    "email": "test@t.com",
    "LOGIN": true,
    "name": "testing",
    "logintype": "ca"
}

Also tried using cURL:

<?php
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
$curl_response = curl_exec($curl);
curl_close($curl);
$curl_jason = json_decode($curl_response, true);
print_r($curl_jason);
?>

But it is not working and it returns empty. i have checked that allow_url_fopen is on.

Community
  • 1
  • 1
Pathik Vejani
  • 4,263
  • 8
  • 57
  • 98

2 Answers2

-1

This is the class I said:

/**
 * Created by PhpStorm.
 * User: rain
 * Date: 15/11/2
 * Time: 下午4:08
 */
class MyCurlLibrary{
    public static function getRequest($url,Array $data=array(),$isNeedHeader=0){
        $ch = curl_init();
        if($data){
            $bindQuery = http_build_query($data);
            $url.="?".$bindQuery;
        }
        curl_setopt($ch, CURLOPT_URL, $url);
        //if need return
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        //this is for https
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_HEADER, $isNeedHeader);//if contains header 
        //this is for web redirect problem
        //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }

    public static function postRequest($url,Array $data=array()){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        // post Data
        curl_setopt($ch, CURLOPT_POST, 1);
        // post variable
        if($data){
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        $output = curl_exec($ch);
        curl_close($ch);
        //return data
        return $output;
    }

}

/**
 * sample
 *
 * //test Data
    $url = "http://localhost:8080/test/test.php";//改文件有代码  print_r($_GET);         print_r($_POST)
    $data = array('a'=>'b','c'=>'d',8=>666,888);
    //test get function
    $result = MyCurl::getRequest($url,$data);
    //test post function
    $result = MyCurl::postRequest($url,$data);
    //print result
    var_dump($result);
 *
 *
 */
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
rain
  • 243
  • 1
  • 8
-1

This might helpful

http://php.net/manual/en/migration56.openssl.php

So code looks like this:

<?php

$arrContextOptions=array(
    "ssl"=>array(
        "cafile" => "/path/to/bundle/ca-bundle.crt",
        "verify_peer"=>false,
        "verify_peer_name"=>false,
    ),
);


$response = file_get_contents("https://test.com/getSessionData", false, stream_context_create($arrContextOptions));

echo $response; ?>
Ninju
  • 2,522
  • 2
  • 15
  • 21