Blank Response on a cURL Request PHP

2019-08-14 16:39发布

问题:

I am making an API request using cURL but I'm getting a blank result. I tried to var_dump the url then tried the url on the browser and works fine. Here's my code.

$parameters = array(
            "user" => urlencode('xxxx'),
            "password" => urlencode('xxxx'),
            "msisdn" => urlencode('09277878067'),
            "type" => urlencode('full'),
        );

        $post_data = '';
        //create name value pairs seperated by &
        foreach($parameters as $k => $v) 
        { 
            $post_data .= $k . '='.$v.'&'; 
        }

        // Remove the & from the last
        $post_data2 = rtrim($post_data, '&');
        $url = "http://www.telcoportal.com/hlr/check?";

        $ch = curl_init();  
        curl_setopt($ch,CURLOPT_URL,$url.$post_data2);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch,CURLOPT_HEADER, false); 
        curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, false); 
        curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, false);   
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); 
        $output=curl_exec($ch);

        curl_close($ch);

        var_dump($output);
        var_dump($url.$post_data2);
        exit();

The $url.$post_data2 is http://www.telcoportal.com/hlr/check?user=xxxx&password=xxxx&msisdn=09277878032&type=full which when you checked the browser gives a response {"status":"error","remark":"Authentication Failed"} . But my $output is blank and not getting anything.

回答1:

You should use curl_getInfo function to get the information about you session. Here is the url http://php.net/manual/en/function.curl-getinfo.php

For your case the test should work.

if(!curl_errno($ch))
{

  $info = curl_getinfo($ch);

  echo 'Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url'];
}

curl_close($ch); ?>



回答2:

Try using echo curl_errno($ch); to get the error code returned from the call.

http://php.net/manual/en/function.curl-errno.php



回答3:

Found it. It's the they use https . it's working now. Sorry for the noob question. thanks



标签: php http curl