简体   繁体   中英

Blank Response on a cURL Request PHP

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.

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); ?>

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM