Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

php curl 413 error

Posted on 2011-03-25
3
Medium Priority
?
1,186 Views
Last Modified: 2012-05-11
Hi,

When using curl in PHP I get 413 errors. Why is that?
It's also weird because the script works on my PHP Version 5.3.1 (cURL Information       7.19.6)
but it doesn't work on:
PHP Version 5.2.9 (cURL Information       libcurl/7.21.4 OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5)

<?php
class RegAppAPI {
   private $APIUsername;
   private $APIPassword;
   
   private $APIUrl;
   
   private $APIContents; //array
   private $curlHandle;
   private $sessionInfo;
   private $lastError;
   private $rawData;
   function __construct($service_list, $username, $password) {
		$this->APIUsername=$username;
	   $this->APIPassword=$password;
	  
	  //setup common cUrl properties
	  $this->curlHandle = curl_init();
	  curl_setopt($this->curlHandle, CURLOPT_HTTPAUTH, CURLAUTH_BASIC ) ;
	  curl_setopt($this->curlHandle, CURLOPT_USERPWD, "$username:$password");
	  curl_setopt($this->curlHandle, CURLOPT_FOLLOWLOCATION, TRUE);
	  curl_setopt($this->curlHandle, CURLOPT_SSLVERSION,3);
	  curl_setopt($this->curlHandle, CURLOPT_SSL_VERIFYPEER, FALSE);
	  curl_setopt($this->curlHandle, CURLOPT_SSL_VERIFYHOST, FALSE);
	  curl_setopt($this->curlHandle, CURLOPT_HEADER, false);
	  curl_setopt($this->curlHandle, CURLOPT_POST, true);    
	  curl_setopt($this->curlHandle, CURLOPT_RETURNTRANSFER, true);
	  
	  if (gettype($service_list)=="string") {

   
	  $this->APIUrl = $service_list;
	  
	  
	  $ret =  $this->_initAPIContents();
	  if (!$ret) {
		 unset($this);
	  }
	  } else if (gettype($service_list)=="object") {
		 $this->APIContents = $service_list;
	  }
   }
   
   function _initAPIContents() {
	  
	   curl_setopt($this->curlHandle, CURLOPT_URL, $this->APIUrl);
	  
	  $val = curl_exec($this->curlHandle);
	  if (curl_error($this->curlHandle)) {
		 if (curl_error($this->curlHandle)) $this->lastError = curl_error($this->curlHandle);
		 return FALSE;
		 
	  }
	  $responseCode = curl_getinfo($this->curlHandle,CURLINFO_HTTP_CODE);
	  
	  if ($responseCode == 401) {
		 $this->lastError = "Client Request Error ($responseCode) Unauthorized connection.  Your API username and password is incorrect.";
		 return FALSE;
	  } else
	  if ($responseCode >= 400 && $responseCode < 500) {
		 $this->lastError = "Client Request Error ($responseCode).";
		 return FALSE;
	  } else
	  if ($responseCode >=500) {
		 $this->lastError = "Server error ($responseCode)."  ;
		 return FALSE;
	  }
	   $this->APIContents = json_decode($val);
   
	  if (function_exists('json_last_error')) {
	   if (json_last_error() ) {
		 
		 $this->lastError = json_last_error();
		 return FALSE;
	   } 
	  }
	  return TRUE;
   
   }
   
   function execute($name, $params=array(), $optional_params = array()) {
	  $this->lastError = NULL;
	  if (!isset($this->APIContents->{$name})) {
		 $this->lastError = "Service/Operation named '$name' is not initialized.";
		 return FALSE;
	  }
	  $operation = $this->APIContents->{$name};;
	  
   
	  $POST_DATA = array();
	  
	  $sess_info = $this->getSessionInfo();
	  if (!$sess_info && $operation->session_info_required) {
		 $this->lastError='There is no session information associated with this object.';
		 return FALSE;
	  }
	  if ($sess_info != NULL) {
	  foreach ($sess_info as $key=>$value) {
		 $POST_DATA[$key] = $value;
	  }
	  }
	  
	  if (isset($operation->parameters)) {
	  foreach ($operation->parameters as $param)  {
		 if (isset($params[$param])) {
			$POST_DATA[$param] = $params[$param];
		 } else {
			$this->lastError='Request has missing parameter ' . $param . '.';
			return FALSE;
		 }
	  }
	  }
	  
	  if (isset($operation->optionalParameters)) {
	  foreach ($operation->optionalParameters as $param) {
		 if (isset($optional_params[$param])) $POST_DATA[$param]=$optional_params;
	  }
	  }
	  $postString = http_build_query($POST_DATA,'','&');
	  
	  curl_setopt($this->curlHandle, CURLOPT_URL, $operation->url);
	  curl_setopt($this->curlHandle, CURLOPT_POSTFIELDS, $postString);
	  $val = curl_exec($this->curlHandle);
	  $this->rawData=$val;
	  $responseCode = curl_getinfo($this->curlHandle,CURLINFO_HTTP_CODE);
	  
	  if ($responseCode == 401) {
		 $this->lastError = "Client Request Error ($responseCode) Unauthorized connection.  Your API username and password is incorrect.";
		 return FALSE;
	  } else
	  if ($responseCode >= 400 && $responseCode < 500) {
		 $this->lastError = "Client Request Error ($responseCode).";
		 return FALSE;
	  } else
	  if ($responseCode >=500) {
		 $this->lastError = "Server error ($responseCode)."  ;
		 return FALSE;
	  }
	  //echo curl_error($this->curlHandle) . "<br/>";
	  $result = json_decode($val);
	  if (function_exists('json_last_error')) {
	  if (json_last_error()) {
		 $this->lastError = "Error in decoding response: $val";
		 return FALSE;
	  }
	  }
	  if ($result) {
		  if (isset($result->{'session_info'})) {
			 $this->sessionInfo = $result->{'session_info'};
			$this->lastError = NULL;
			return TRUE;
			
		 }
		  if (isset($result->{'result'})) {
			$this->lastError = NULL;
			 return $result->{'result'};
		 
		 }
		 
		 if (isset($result->{'error'})) {
			 $this->lastError = NULL;
			$this->_processError($result->{'error'});
			return FALSE;
		 }
	  }
	  
	  return FALSE;
	  
   }
   function getRawData() {
	  return $this->rawData;
   }
   function _processError($error) {
	  $this->lastError = $error->{'message'};
   }
   function getLastError() {
	  return $this->lastError;
   }
   
   function setSessionInfo($sess_info) {
	  $this->sessionInfo = $sess_info;
   }
   
   function getSessionInfo() {
	   if ($this->sessionInfo) return $this->sessionInfo;
	  
   }
   function close() {
	  curl_close($this->curlHandle);
   
   }
   function getAPIContents() {
	  //$copy = array();
	  //foreach ($this->APIContents as $index=>$content) $copy[$index] = $content;
	  return $this->APIContents;
   }
   
	function showAPIContents() {
	  print "<h2>RegApp API Contents</h2>";
	  if ($this->APIContents) {
	  foreach ($this->APIContents as $name=>$details) {
		 $content =  "<div>";
		$content .= "<p><u><b>$name</b></u></p>";
		$content .= "<p><b>Description: </b> " . $details->{'description'} . "<br/>" ;
		$content .= "<b>Session Info Required :</b> " . (($details->{'session_info_required'}) ? "YES" : "NO") . "<br/>";
		$content .= "<b>Parameters : </b>  " . ((isset($details->{'parameters'}) && count($details->{'parameters'}) > 0) ? implode(", " , $details->{'parameters'}) : "NONE") . "<br/>";
		$content .= "<b>Optional Parameters : </b>  " . ((isset($details->{'optionalParameters'}) && count($details->{'optionalParameters'}) > 0) ? implode(", " , $details->{'optionalParameters'}) : "NONE") . "<br/>";
		$content .= "<b>Returns: </b> "  . $details->{'returns'} . "<br/>";
		$content .= "</div>";
		echo $content;
	  
	  }
	  } else echo "<p>API has no contents.</p>";
   
   }
   
}
?>

Open in new window

0
Comment
Question by:rationalboss
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 7

Expert Comment

by:Swafnil
ID: 35215773
It seems as if PHP 5.2 and 5.3 CURL behave differently, although I haven't experienced the problems myself. A search on Google gave me a comparable error where calling the Twitter API from 5.2 worked and 5.3 didn't, perhaps it'll help you figure out what Curl-settings behave differently now:

http://code.google.com/p/twitter-api/issues/detail?id=1291

Another post shows that there was a bug in the curl_wrappers below PHP 5.2.11, so it would be a good idea to update to the most recent PHP 5.2.17:

http://core.trac.wordpress.org/ticket/11888 (see comment 28)

Good luck!
0
 
LVL 8

Accepted Solution

by:
rationalboss earned 0 total points
ID: 35217707
I was able to solve the problem by changing line 26 to:

	  curl_setopt($this->curlHandle, CURLOPT_POST, false); 

Open in new window

0
 
LVL 8

Author Closing Comment

by:rationalboss
ID: 35252791
i solved it myself
0

Featured Post

Optimum High-Definition Video Viewing and Control

The ATEN VM0404HA 4x4 4K HDMI Matrix Switch supports 4K resolutions of UHD (3840 x 2160) and DCI (4096 x 2160) with refresh rates of 30 Hz (4:4:4) and 60 Hz (4:2:0). It is ideal for applications where the routing of 4K digital signals is required.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question