简体   繁体   English

如何对 PHP 中的 Curl 进行故障排除?

[英]How do I troubleshoot Curl in PHP?

I have never used Curl but I am trying to complete a self api project at my job just to gain some experience.我从未使用过 Curl 但我正在尝试在我的工作中完成一个自己的 api 项目只是为了获得一些经验。 And I am stuck on the first step... I want to authenticate with an api.我被困在第一步......我想用 api 进行身份验证。 So I am running this code and I expect to see a Success 200 response with my access token, etc but I get nothing.因此,我正在运行此代码,并且希望看到带有访问令牌等的 Success 200 响应,但我什么也没得到。 No error, no feedback, the page just opens up blank I have tired to use CURLINFO_HEADER_OUT from this page What Steps do you Take to Troubleshoot Problems with PHP cURL?没有错误,没有反馈,页面只是打开空白我已经厌倦了从这个页面使用 CURLINFO_HEADER_OUT 你采取什么步骤来解决 PHP cURL 的问题? but still I got a blank page但我仍然有一个空白页
Anyway thank you to anyone in advantage for some tips无论如何,感谢任何有优势的人提供一些提示

<?php
const TOKEN_ENDPOINT  = 'xxxxxx';
const GRANT_TYPE      = 'xxxxx';
const CLIENTID        = 'xxxxxxxxxxx';
const CLIENTSECRET    = 'xxxxxxxxxxxxxx';
const USERNAME        = 'xxxxxxxxxxxxxxxxx';
const PASSWORD        = 'xxxxxxxxxxxxxxx';

$clientCredentials = base64_encode(CLIENTID . ':' . CLIENTSECRET);

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => TOKEN_ENDPOINT,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS =>'grant_type=' . GRANT_TYPE . '&username=' . USERNAME . '&password=' . PASSWORD ,
  CURLOPT_HTTPHEADER => array(
    'Content-Type: application/x-www-form-urlencoded',
    'Accept: application/json',
    'Authorization: Basic ' . $clientCredentials
  ),
));


$response = curl_exec($curl);
curl_close($curl);
echo $response ;
?>

To check curl error, the best way is to use curl_error function要检查 curl 错误,最好的方法是使用curl_error function

$response = curl_exec($curl);
if (curl_errorno($curl)) {
    $error_msg = curl_error($curl);
}
curl_close($curl);
echo $response ;

See the description of libcurl error codes here在此处查看 libcurl 错误代码的描述

See the description of PHP curl_errno() function here请参阅 PHP curl_errno() function 这里的描述

See the description of PHP curl_error() function here在此处查看 PHP curl_error() function 的描述

When all else fails, i do this (code snippet from my ApiHelper curl wrapper).当所有其他方法都失败时,我会这样做(来自我的 ApiHelper curl 包装器的代码片段)。 Use your own logger or evidence printing mechanism.使用您自己的记录器或证据打印机制。 Most every time the answer to the puzzle is in the printed stuff:大多数时候,谜题的答案都在印刷品中:

        // we simply stream curlopt debug info to a temporary 
        // file, so we can log it out later (when helper is set
        // to verbose)
        $st       = microtime(true);
        $verbiage = null;
        if ($this->verbose) {
            // write out the curl debug stuff
            curl_setopt($ch , CURLINFO_HEADER_OUT , false);
            curl_setopt($ch , CURLOPT_VERBOSE , true);
            $verbiage = fopen('php://temp' , 'w+');
            curl_setopt($ch , CURLOPT_STDERR , $verbiage);
        }

        $resp                = curl_exec($ch);
        $end                 = microtime(true);           // get as float
        $delta               = 1000.0 * ($end - $st);    // treat as float
        $this->roundTripInMs = sprintf("%.2f" , $delta);
        $this->getInstanceLogger()->debug("WS call : round trip took " . sprintf("%.2f" , $delta) . " ms.");
     
        if ($this->verbose) {
            // rewind and log the verbose output
            rewind($verbiage);
            $verboseLog = stream_get_contents($verbiage);
            $this->getInstanceLogger()->info("Verbose cURL : \n$verboseLog");
            fclose($verbiage);
        }
        $this->curlinfo = curl_getinfo($ch);
        $this->verbose && $this->getInstanceLogger()->info("cURL info : \n" . json_encode($this->curlinfo , PEHR_PRETTY_JSON));
        $this->httpStatus = curl_getinfo($ch , CURLINFO_RESPONSE_CODE);
        if (!$resp) {
            if ($this->verbose) $this->getInstanceLogger()->error("CURL request to [$url] failed\n" . json_encode($this->curlinfo , PEHR_PRETTY_JSON));
            return 0;
        }
        curl_close($ch);
        if ($resp && $this->verbose) $this->getInstanceLogger()->debug("Received json \n" . $resp);

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

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