isleshocky77

Debugging cURL

Mar 15th, 2012
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.23 KB | None | 0 0
  1. /* .. */
  2. /* curl_init() part of send_request() */
  3.  
  4.     $curl = curl_init($url);
  5.     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  6.     curl_setopt($curl, CURLOPT_FAILONERROR, false);
  7.     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  8.     curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
  9.     curl_setopt($curl, CURLOPT_POST, 1);
  10.     curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
  11.  
  12.     //* Debugging
  13.     curl_setopt($curl, CURLOPT_VERBOSE, true);
  14.     //*/
  15.  
  16.     $response           = curl_exec($curl);
  17.     $responseCode       = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  18.     $responseContenType = curl_getinfo($curl, CURLINFO_CONTENT_TYPE);
  19.     //* Debugging
  20.     echo '=================== CURLINFO =====================' .PHP_EOL;
  21.     echo $http_method . ' ' . $url .PHP_EOL;
  22.     echo var_export(curl_getinfo($curl), true) .PHP_EOL;
  23.     echo '=================== POST DATA ====================' .PHP_EOL;
  24.     if(stristr($postData, 'xml')) {
  25.       echo myPrintTool::formatXml($postData) .PHP_EOL;
  26.     }elseif(json_decode($response)) {
  27.       echo var_export(json_decode($postData), true) .PHP_EOL;
  28.     } else {
  29.       echo $postData .PHP_EOL;
  30.     }
  31.     echo '=================== RESPONSE =====================' .PHP_EOL;
  32.     if(stristr($responseContenType, 'xml')) {
  33.       echo myPrintTool::formatXml($response) .PHP_EOL;
  34.     }elseif(stristr($responseContenType, 'json')) {
  35.       echo var_export(json_decode($response), true) .PHP_EOL;
  36.     } else {
  37.       echo $response .PHP_EOL;
  38.     }
  39.     if (!$response) {
  40.       echo '=================== ERROR ========================' .PHP_EOL;
  41.       echo curl_error($curl);
  42.     }
  43.  
  44.  
  45. /* This is just a helper class for formatting the xml */
  46. class myPrintTool {
  47.  
  48.   static function formatXml($xml, $htmlOutput = false, $tidy_options = array()) {
  49.  
  50.     $default_tidy_options = array(
  51.       'input-xml'    => true,
  52.       'output-xml'   => true,
  53.       'indent'       => true,
  54.       'wrap'         => false,
  55.     );
  56.     $tidy_options = array_merge($default_tidy_options, $tidy_options);
  57.  
  58.     $tidy = new tidy();
  59.     $tidy->parseString($xml, $tidy_options);
  60.     $tidy->cleanRepair();
  61.  
  62.     if($htmlOutput) {
  63.       $tidy = '<pre>'.htmlentities($tidy).'</pre>';
  64.     }
  65.  
  66.     return $tidy;
  67.   }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment