简体   繁体   English

cURL错误6:无法解析主机-Guzzle

[英]cURL error 6: Couldn't resolve host - Guzzle

Description 描述

guzzle version 6.3 狂饮版本6.3

If I have this in my class 如果我班上有这个

class CURL { CURL类{

public static function get($url) {

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_TIMEOUT_MS, 1000);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); //<------ Note HERE 
    curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
    $result = curl_exec($ch);
    curl_close($ch);
    $result =  json_decode($result, true);
    return $result;

}

If I have this in my class 如果我班上有这个

Try #1 尝试#1

<?php
namespace App;

use Request,Auth;
use App\Log, App\Helper;

use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Client;

use GuzzleHttp\FORCE_IP_RESOLVE;
use GuzzleHttp\DECODE_CONTENT;
use GuzzleHttp\CONNECT_TIMEOUT;
use GuzzleHttp\READ_TIMEOUT;
use GuzzleHttp\TIMEOUT;

use GuzzleHttp\VERIFY;



class CURL {

    $client = new Client();
    $options = [
        'http_errors' => false,
        'force_ip_resolve' => 'v4',
        'connect_timeout' => 10,
        'read_timeout' => 10,
        'timeout' => 10,
        'verify' => false
    ];
    $result = $client->request('GET',$url,$options);
    $result = json_decode((string) $result->getBody(), true);
    return $result;
    ...

}

As you can see, I tried set 'verify' => false already. 如您所见,我已经尝试设置'verify' => false

I got 我有

在此处输入图片说明


Try #2 尝试#2

add 'curl' => [ CURLOPT_SSL_VERIFYPEER => false ] 添加'curl' => [ CURLOPT_SSL_VERIFYPEER => false ]

    $client = new Client();
    $options = [
        'http_errors' => false,
        'force_ip_resolve' => 'v4',
        'connect_timeout' => 10,
        'read_timeout' => 10,
        'timeout' => 10,
        'verify' => false,
        'curl' => [ CURLOPT_SSL_VERIFYPEER => false ]
    ];
    $result = $client->request('GET',$url,$options);
    $result = json_decode((string) $result->getBody(), true);
    return $result;

I got 我有

在此处输入图片说明


Try #3 尝试#3

removing 'force_ip_resolve' => 'v4', 删除'force_ip_resolve' => 'v4',

    $client = new Client();
    $options = [
        'http_errors' => false,
        'connect_timeout' => 10,
        'read_timeout' => 10,
        'timeout' => 10,
        'verify' => false
    ];
    $result = $client->request('GET',$url,$options);
    $result = json_decode((string) $result->getBody(), true);
    return $result;

I got 我有

在此处输入图片说明


Try #4 尝试#4

adding this line $result = $result->getCurlOptions()->set(CURLOPT_SSL_VERIFYPEER, FALSE); 添加这行$result = $result->getCurlOptions()->set(CURLOPT_SSL_VERIFYPEER, FALSE);

    $client = new Client();
    $options = [
        'http_errors' => false,
        'force_ip_resolve' => 'v4',
        'connect_timeout' => 10,
        'read_timeout' => 10,
        'timeout' => 10,
        'verify' => false
    ];
    $result = $client->request('GET',$url,$options);
    $result = $result->getCurlOptions()->set(CURLOPT_SSL_VERIFYPEER, FALSE);
    $result = json_decode((string) $result->getBody(), true);
    return $result;

I got 我有

Call to undefined method GuzzleHttp\\Psr7\\Response::getCurlOptions() 调用未定义的方法GuzzleHttp \\ Psr7 \\ Response :: getCurlOptions()


Questions 问题

How would one go about and debug this further ? 人们将如何进行进一步调试?


I'm open to any suggestions at this moment. 目前,我愿意接受任何建议。

Any hints/suggestions / helps on this be will be much appreciated! 任何提示/建议/帮助将不胜感激!

the problem here is that $url is NOT a valid url, in fact, it seems your $url variable contains html. 这里的问题是$ url不是有效的url,实际上,您的$ url变量似乎包含html。 then you tell curl to connect to... this html, as an url, and curl says it cant find that domain. 然后您告诉curl连接到...此html,作为url,curl表示找不到该域。 try giving a valid url (for example, hhtp://example.org ) as the $url parameter, and it should work fine. 请尝试提供有效的网址(例如, hhtp://example.org )作为$ url参数,它应该可以正常工作。

The issue here is that you're trying to set the verify option on a response, at least for your third example 这里的问题是,至少在第三个示例中,您尝试设置响应的verify选项

You can disable ssl verification in guzzle like so: 您可以像这样禁用ssl验证:

$client->getHttpClient()->setDefaultOption('verify', false); -- which actually, you already have, so in theory, you don't even need the line: -实际上,您已经有了,因此从理论上讲,您甚至不需要该行:

$result = $result->getCurlOptions()->set(CURLOPT_SSL_VERIFYPEER, FALSE);

As for #2, curl is indicating that you're passing the HTML of a page to the get method, not the URL, hence the unknown host error. 对于#2,curl表示您正在将页面的HTML传递给get方法,而不是URL,因此是未知的主机错误。

You were also calling json_decode on the response body, which isn't text, you need $response->getBody()->getContents(); 您还在响应正文上调用了json_decode,它不是文本,您需要$response->getBody()->getContents(); , like so ,像这样

public static function get($url){
    $options = [
        'http_errors' => false,
        'force_ip_resolve' => 'v4',
        'connect_timeout' => 10,
        'read_timeout' => 10,
        'timeout' => 10,
        'verify' => false
    ];
    $client = new Client($options);
    $result = $client->request('GET',$url);
    $result = json_decode($result->getBody()->getContents(), true);
    return $result;
}

You have 'force_ip_resolve' => 'v4', in all Guzzle's examples, but don't have the same option for cURL example. 在所有Guzzle的示例中,您都具有'force_ip_resolve' => 'v4', ,但对于cURL示例,没有相同的选项。 Try to remove it, unless you really need it. 除非您确实需要,否则请尝试将其删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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