Export Contacts Using PHP Wrapper Class Returning Error

SamranN
Occasional Participant

Export Contacts Using PHP Wrapper Class Returning Error

Hi All,

 

i am new to CC APIs and i am using a wrapper class that i got from here  www.integrationservic.es for php developer

but the thing is when i used its export_contacts example it is continously returing an error that is 

 

Operation failed: Invalid Request - There are many possible causes for this error, but most commonly there is a problem with the structure or content of XML your application provided. Carefully review your XML. One simple test approach is to perform a GET on a URI and use the GET response as an input to a PUT for the same resource. With minor modifications, the input can be used for a POST as well.

 

and the code for it is as follow

 

function get_export_file_columns()
{
$columns = array(
'FIRST NAME',
'MIDDLE NAME',
'LAST NAME',
'JOB TITLE',
'COMPANY NAME',
'WORK PHONE',
'HOME PHONE',
'ADDRESS LINE 1',
'ADDRESS LINE 2',
'ADDRESS LINE 3',
'CITY',
'STATE',
'STATE/PROVINCE (US/CANADA)',
'COUNTRY',
'POSTAL CODE',
'SUB POSTAL CODE',
);

$new = array();

foreach($columns as $column):
$new[$column] = $column;
endforeach;

return $new;
}

 

function export_contacts($list_id, $export_type = 'CSV', $columns = array(), $sort_by = 'DATE_DESC')
{
if(!is_array($columns) || !count($columns)):
$columns = $this->get_export_file_columns();
endif;

$params['activityType'] = 'EXPORT_CONTACTS';
$params['fileType'] = $export_type;
$params['exportOptDate'] = 'true';
$params['exportOptSource'] = 'true';
$params['exportListName'] = 'false';
$params['sortBy'] = $sort_by;
$params['columns'] = $columns;
$params['listId'] = $this->get_list_url($list_id);

$this->http_set_content_type('application/x-www-form-urlencoded');

$xml = $this->load_url("activities", 'post', $params, 201);

if($xml):
return true;
endif;

/*
if(isset($this->http_response_headers['Location']) && trim($this->http_response_headers['Location']) != ''):
return $this->get_id_from_link($this->http_response_headers['Location']);
endif;
*/

return false;
}

 

function get_list_url($id, $full_address = true)
{
if($full_address):
$_url = str_replace('https:', 'http:', $this->api_url . "lists");
else:
$_url = $this->api_uri . "lists";
endif;

return "$_url/$id";
}

 

function http_set_content_type($content_type)
{
$this->http_content_type = $content_type;
}

 

function load_url($action = '', $method = 'get', $params = array(), $expected_http_code = 200)
{
$this->http_send($this->api_url . $action, $method, $params);

// handle status codes
if(intval($expected_http_code) === $this->http_response_code):
if($this->http_content_type):
return $this->xml_to_array($this->http_response_body);
else:
return $this->http_response_body; /* downloads the file */
endif;
else:
$this->last_error = "Invalid status code {$this->http_response_code}";

// if their was an error sometimes the body contains useful info
return false;
endif;
}

 

function http_send($the_url, $method, $params = array())
{
$this->http_response = '';
$this->http_response_code = '';
$this->http_method = $method;

$method = strtoupper($method);

$ch = curl_init($the_url);
curl_setopt($ch, CURLOPT_USERAGENT, $this->http_user_agent);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $this->http_user . ':' . $this->http_pass);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: {$this->http_content_type}"));
curl_setopt($ch, CURLOPT_TIMEOUT, $this->http_request_timeout);

if($method == 'POST' OR $method == 'PUT' AND count($params) > 0):
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
endif;

curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);

$this->http_response_body = curl_exec($ch);
$this->http_response_info = curl_getinfo($ch);
$this->http_response_error = curl_error($ch);
$this->http_response_code = $this->http_response_info['http_code'];
}

 

Please help in resolving the issue. bundle of thanks in advance

 

Thanks

1 REPLY 1
Mark-C
Member

Hello,

 

I'm not familiar with the wrapper library you're using, as it's a third-party development, but the error message returned by their library looks like a translation/elaboration of the 400 error response from our end. The trouble with the error translated by wrapper libraries is that they don't typicallyt include the body of the response, which includes more specific information about the problem in the XML. These problems can be in the structure of the XML, but with established wrapper libraries more often tend to be in the contents of one or more elements in the XML, instead.

 

If you can add a var_dump() function so you can grab the XML, and mock up the request being made in RESTClient (see here), you should be able to get more specific information about what's causing the error, but in the event that still doesn't help you identify the problem, email the XML to us (as a text file attachment), and we can try to do some additional troubleshooting to help you find the source of the problem.

Mark Coleman
Support Engineer
Developer Portal

View API documentation, code samples, get your API key.

Visit Page