Home  >  Article  >  Backend Development  >  PHP curl error: curl__errno() returns error code 6

PHP curl error: curl__errno() returns error code 6

WBOY
WBOYOriginal
2016-12-01 00:57:016263browse

When I used curl to call the interface to obtain data, curl_errno() returned the error code '6'. Baidu looked it up

<code>CURLE_COULDNT_RESOLVE_HOST (6)
Couldn't resolve host. The given remote host was not resolved.</code>

It turns out that the program can normally obtain the data returned by the interface. This situation occurred suddenly. I wonder if the API restricts the call? I don’t know the specific solution yet. If you encounter the same situation, you can share the solution. Thank you.
PS: The data can be obtained directly by accessing the interface url in the browser.
curl code:

<code>public function getApiDataWithCurl($params = array()){
        $doc = array(
            'result'=>0,
            'content'=>'',
        );
        if(!isset($params['feed_id'])) return $doc;
        if(!isset($params['apikey'])) return $doc;
        $getUrl = $this->apiUrl.'?';
        foreach($params as $k => $v){
            if($v != ''){
                $getUrl .=$k.'='.$v.'&';
            }
        }
        $getUrl = substr($getUrl,0,strlen($getUrl)-1);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$getUrl);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, false);
        $response_content = curl_exec($ch);
        $error_code = curl_errno($ch);
        $curl_info = curl_getinfo($ch);
        curl_close($ch);
        $response_content = simplexml_load_string($response_content);
        $json = json_encode($response_content);
        $response_content = json_decode($json,TRUE);
        if($error_code || (!$response_content && $curl_info['http_code']!=200)){
            return 'CURL ERROR: error code '.$error_code;
        }else{
            $doc = array(
                'result'=>1,
                'content'=>$response_content,
            );
            return $doc;
        }
    }</code>

Reply content:

When I used curl to call the interface to obtain data, curl_errno() returned the error code '6'. Baidu looked it up

<code>CURLE_COULDNT_RESOLVE_HOST (6)
Couldn't resolve host. The given remote host was not resolved.</code>

It turns out that the program can normally obtain the data returned by the interface. This situation occurred suddenly. I wonder if the API restricts the call? I don’t know the specific solution yet. If you encounter the same situation, you can share the solution. Thank you.
PS: The data can be obtained directly by accessing the interface url in the browser.
curl code:

<code>public function getApiDataWithCurl($params = array()){
        $doc = array(
            'result'=>0,
            'content'=>'',
        );
        if(!isset($params['feed_id'])) return $doc;
        if(!isset($params['apikey'])) return $doc;
        $getUrl = $this->apiUrl.'?';
        foreach($params as $k => $v){
            if($v != ''){
                $getUrl .=$k.'='.$v.'&';
            }
        }
        $getUrl = substr($getUrl,0,strlen($getUrl)-1);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$getUrl);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, false);
        $response_content = curl_exec($ch);
        $error_code = curl_errno($ch);
        $curl_info = curl_getinfo($ch);
        curl_close($ch);
        $response_content = simplexml_load_string($response_content);
        $json = json_encode($response_content);
        $response_content = json_decode($json,TRUE);
        if($error_code || (!$response_content && $curl_info['http_code']!=200)){
            return 'CURL ERROR: error code '.$error_code;
        }else{
            $doc = array(
                'result'=>1,
                'content'=>$response_content,
            );
            return $doc;
        }
    }</code>

This is a problem of being unable to resolve the domain name. Try typing out the URL and see if you can ping it

Can you post curl

Look, this is a GET value request! You can try using file_get_content directly! Has the other party set up a whitelist?

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn