您采取什么步骤来解决PHP cURL的问题? [英] What Steps do you Take to Troubleshoot Problems with PHP cURL?

查看:142
本文介绍了您采取什么步骤来解决PHP cURL的问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几乎任何正在工作的PHP程序员都面临着使用CURL发送原始HTTP请求,无论是用于信用卡支付处理,恶意屏幕抓取,还是介于其间的东西。

Almost any working PHP programmer has faced having to use CURL to send raw HTTP requests, whether it's for credit card payment processing, nefarious screen scraping, or something in-between.

几乎所有的PHP程序员聚集的论坛都有大量的人无法获取cURL函数来做他们想要的

Almost any forum where PHP programmers congregate has a large number of people who can't get the cURL functions to do what they want.

当cURL不工作时,你使用什么故障排除技术来找出它为什么不工作?有什么奇怪的问题与PHP的curl实现你遇到了?如果有人在论坛上询问HALP MY CURL IZ BROKEN问题,您需要采取哪些步骤来找出他们的请求无效的原因?

When cURL isn't working for you, what troubleshooting techniques do you use to figure out why it isn't working? What weird gotchas with PHP's curl implementation have you run into? If someone asks a "HALP MY CURL IZ BROKEN" question on a forum, what are the steps you take to figure out why their request isn't working?

推荐答案

我发现CURLINFO_HEADER_OUT选项非常有用。

I find the CURLINFO_HEADER_OUT option to be very useful.

<?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 cURL的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆