php - 您采取了哪些步骤来解决 PHP cURL 问题?

标签 php debugging http curl

几乎所有工作的 PHP 程序员都必须使用 CURL 发送原始 HTTP 请求,无论是信用卡支付处理、恶意屏幕抓取,还是介于两者之间。

几乎任何PHP程序员聚集的论坛都有大量的人can't get the cURL functions to do what they want .

当 cURL 不适合您时,您使用什么故障排除技术来找出它不工作的原因?您在 PHP 的 curl 实现中遇到过哪些奇怪的问题?如果有人在论坛上提出“HALP MY CURL IZ BROKEN”问题,您采取了哪些步骤来弄清楚为什么他们的请求不起作用?

最佳答案

我发现 CURLINFO_HEADER_OUT 选项非常有用。

<?php
$curl = curl_init('http://www.php.net');

curl_setopt($curl, CURLOPT_HEADERFUNCTION, 'dbg_curl_data');
curl_setopt($curl, CURLOPT_WRITEFUNCTION, 'dbg_curl_data');
curl_setopt($curl, CURLINFO_HEADER_OUT, true);

curl_exec($curl);

echo '<fieldset><legend>request headers</legend>
  <pre>', htmlspecialchars(curl_getinfo($curl, CURLINFO_HEADER_OUT)), '</pre>
</fieldset>';

echo '<fieldset><legend>response</legend>
  <pre>', htmlspecialchars(dbg_curl_data(null)), '</pre>
</fieldset>';

function dbg_curl_data($curl, $data=null) {
  static $buffer = '';

  if ( is_null($curl) ) {
    $r = $buffer;
    $buffer = '';
    return $r;
  }
  else {
    $buffer .= $data;
    return strlen($data);
  }
}

关于php - 您采取了哪些步骤来解决 PHP cURL 问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/815910/

相关文章:

java - 本地路径的 OSGi httpService.registerResources()

objective-c - NSURLConnection 在约 5 分钟后忽略服务器响应——没有超时

php - 使用非英语值更新 Amazon S3 对象的元数据

javascript - 将控制台输出的代码更改为 JSON 输出

php - MySQL SELECT 查询语法 - 没有按预期工作?

visual-studio - Monodroid 调试不会显示超过 100 个字符的字符串

c++ - 用于调试内存使用情况的工具?

angular - 覆盖 Angular httpclient get 请求上的基本 URL

php - 如何使用一个或多个搜索词搜索字符串并在每次匹配后添加一个字符串?

php - 如何在php中通过SSL连接Mysql数据库?