【问题标题】:Making GET requests using PHP使用 PHP 发出 GET 请求
【发布时间】:2014-01-28 20:50:09
【问题描述】:

我正在尝试向 yesmail api 发出一个简单的 GET 请求,我有可以直接粘贴到浏览器中的 url,它会显示正确的信息,并且如果我添加以下代码,如简要文档中所示在 Firefox RESTClient 我得到正确的响应:

GET https://services.yesmail.com/enterprise/subscribers?email=karina@email.co.uk HTTP/1.1
Accept-Encoding: gzip,deflate
User-Agent: Jakarta Commons-HttpClient/3.1
Authorization: Basic xxxxxxxxxxxxx
Host: services.yesmail.com

但是,当尝试使用 CURL 进行连接时,我什么也得不到,根本没有 HTTP 响应,只是一个空白页面。我不确定我做错了什么?这就是我所拥有的:

$url = "https://services.yesmail.com/enterprise/subscribers?email=karina@email.co.uk";  
$header[] = "Accept-Encoding: gzip, deflate";
$header[] = "User Agent: Jakarta Commons-HttpClient/3.1";
$header[] = "Authorization: Basic xxxxxxxxxxxxx";
$header[] = "Host: services.yesmail.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header );
curl_setopt($ch, CURLOPT_USERPWD, 'xxxxx:xxxxxxxxxx');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
$response = (curl_exec($ch));
print_r($response);
curl_close($ch);

我是否认为我输入到 RESTClient 的信息会进入 CURL 的标头中?这是我第一次使用 API,因此感谢您的帮助。

更新 当使用 file_get_contents 函数时,我得到了正确的响应(对于 GET 方法,它只是一个带有我唯一订户号的 URL):

 $context = stream_context_create(array(
'http' => array(
    'header'  => "Authorization: Basic " . base64_encode("*******:********")
)
));
$data = file_get_contents('https://services.yesmail.com/enterprise/subscribers?email=karina@email.co.uk', false, $context);

 echo $data;

但是我真的想让 CURL 方法正常工作,因为我希望能够添加订阅者。

【问题讨论】:

    标签: php api http curl get


    【解决方案1】:

    我实际上在 Yesmail 工作,所以我可以在这里为您提供帮助。

    试试下面的代码(确保将your-auth-here 文本替换为您的base64 编码身份验证信息,并在$ym_api_url 中设置适当的URL)

    <?php
    
    // Set the HTTP headers needed for this API call.
    $http_headers = array(
        "Authorization: Basic your-auth-here",
        "Accept: application/json",
        "Connection: close",                    // Disable Keep-Alive
        "Expect:"                               // Disable "100 Continue" server response
    );
    
    // URL Endpoint of the API to be called.
    $ym_api_url = 'https://services.yesmail.com/enterprise/subscribers?email=karina@email.co.uk';
    
    $curl_hdl = curl_init();
    
    $curl_options = array(
        CURLOPT_VERBOSE => 1,                 // Verbose mode for diagnostics
        CURLOPT_HEADER => TRUE,               // Include the header in the output.
        CURLOPT_HTTPHEADER => $http_headers,  // HTTP headers to set
        CURLOPT_HTTPAUTH => CURLAUTH_BASIC,   // Use basic authentication.
        CURLOPT_PROTOCOLS => CURLPROTO_HTTPS, // Bitmask of protocols libcurl may use in this transfer
        CURLOPT_RETURNTRANSFER => TRUE,       // Return result as return value of curl_exec()
        CURLOPT_URL => $ym_api_url,           // URL to POST data to
    );
    
    curl_setopt_array($curl_hdl, $curl_options);
    
    // Make the API call
    $response = curl_exec($curl_hdl);
    
    // Get the http response code
    $http_response_code = curl_getinfo($curl_hdl, CURLINFO_HTTP_CODE);
    
    curl_close($curl_hdl);
    
    echo PHP_EOL . "INFO: HTTP Response Code was: " . $http_response_code . PHP_EOL;
    
    if ( $response === false )
    {
        echo PHP_EOL . "ERROR: curl_exec() has failed." . PHP_EOL;
    }
    else
    {
        echo PHP_EOL . "INFO: Response Follows..." . PHP_EOL;
        echo PHP_EOL . $response;
    }
    
    ?>
    

    CURLOPT_VERBOSE =&gt; 1 选项将输出额外的诊断信息。注意那里的任何问题,看看是否指向特定问题。解决问题后,您可以删除或禁用这些选项。

    另外,请记住,您可以随时联系您的 Yesmail 客户经理并寻求帮助。

    【讨论】:

    • 太好了,谢谢你的帮助,我使用了你提供的代码,现在我得到了一些东西 INFO: HTTP Response Code was: 0 ERROR: curl_exec() has failed. 所以 HTTP 响应 0,查了一下,发现有时会发生这种情况,当调用在得到响应之前被取消时?我尝试使用函数file_get_contents 只是为了看看我是否可以从中得到任何响应并且它工作正常(我已经用我使用的代码更新了我的问题)
    • 使用选项 CURLOPT_VERBOSE => 1 执行此操作时,您应该已经收到一些诊断消息(写入 STDERR)。这些消息(理论上)应该已经给出了一些关于问题所在的指示。您可以在编辑帖子时提供这些诊断消息吗?如果从网页(而不是命令行)执行此操作,您可能需要通过一些额外的工作来获取消息;有关如何执行此操作的信息,请参阅 this answer
    【解决方案2】:

    所以你得到了$response,但不要对它做任何事情。

    您的代码中没有一行可以显示任何内容。

    更新:我看到你更新了你的问题。

    一条非常重要的规则:始终检查 API 调用的返回值。

    curl_exec() 通过返回 FALSE 来表示错误,如果是这种情况,请使用 curl_error($ch) 进行检查。

    【讨论】:

    • 好的,我添加了,但我仍然只返回一个空白页?没有错误,什么都没有。
    • 你在浪费我们的时间。请提供重现问题的代码。
    猜你喜欢
    • 2021-12-09
    • 1970-01-01
    • 1970-01-01
    • 2020-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-25
    • 1970-01-01
    相关资源
    最近更新 更多