>  기사  >  백엔드 개발  >  curl 怎么保留前面的 0

curl 怎么保留前面的 0

WBOY
WBOY원래의
2016-06-06 20:39:12904검색

<code> private function _sendRequest($url)
    {
        $content = "";
        if(function_exists('file_get_contents'))
        {
            $content = @file_get_contents($url);
        }
        else
        {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); //allow redirects
            curl_setopt($ch, CURLOPT_HEADER, false);
            $content = curl_exec($ch);
            $error = curl_errno($ch);
            $info = curl_getinfo($ch);
            curl_close($ch);
        }
        $contenArr = json_decode($content);

        if(json_last_error() === JSON_ERROR_NONE)
        {
            return $contenArr;
        }
        else
        {
            return $content;
        }

    } 
</code>

直接在浏览器敲返回的是 0001 用这方法返回的是1 这么保留前面的0

回复内容:

<code> private function _sendRequest($url)
    {
        $content = "";
        if(function_exists('file_get_contents'))
        {
            $content = @file_get_contents($url);
        }
        else
        {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); //allow redirects
            curl_setopt($ch, CURLOPT_HEADER, false);
            $content = curl_exec($ch);
            $error = curl_errno($ch);
            $info = curl_getinfo($ch);
            curl_close($ch);
        }
        $contenArr = json_decode($content);

        if(json_last_error() === JSON_ERROR_NONE)
        {
            return $contenArr;
        }
        else
        {
            return $content;
        }

    } 
</code>

直接在浏览器敲返回的是 0001 用这方法返回的是1 这么保留前面的0

json_decode干的好事
$content本来是0001,你json_decode($content)把它转换成数值1了。

你确定执行了curl 吗,为什么不是file_get_contents呢?
json_decode是用来对 JSON 格式的字符串进行编码的,那么请问,你那个是json字符串吗?是用双引号包围这的吗?

curl 怎么保留前面的 0

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.