Reputation: 11
I have a project written in plain php by using curl and it’s worked. Now I want to move this project to zend framework 1 (and I am new to zend). I have tried to connect by Zend_Http_Client_Adapter_Curl and I did not get enough information what I need My plain php code: function server_com($data, $api_host) {
$xml = "xml=".($data);
$host = $api_host;
//curl initialization
$cpt = curl_init();
//curl url
curl_setopt($cpt, CURLOPT_URL, "https://$host");
curl_setopt($cpt, CURLOPT_SSL_VERIFYHOST, 1);
//Return the response as a string instead of outputting it to the screen
curl_setopt($cpt, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($cpt, CURLOPT_SSL_VERIFYPEER, FALSE);
// set this true if you want to send a POST request
curl_setopt($cpt, CURLOPT_POST, 1);
//Array of data to POST in request
curl_setopt($cpt, CURLOPT_POSTFIELDS, array('xml' => $data));
//curl execution
$result = curl_exec($cpt);
RETURN $result;
}
$data = '<?xml version="1.0" encoding="utf-8"?>
<Request version="1.0">
<Export mode="UPDATE" type="COREDATA">
<Login>
<User>user</User>
<Password>password</Password>
</Login>
</Export>
</Request>
';
// specifies the URL for the request
$api_host = "demo.api.net/api/";
$result = server_com($data, $api_host);
$fh = fopen("va.xml", "w");
fwrite($fh,$result);
fclose($fh);
here i am creating va.xml file after query resquest. now, I want this equivalent to Zend framework 1 by using cURL adapter, Can someone please help? I have tried to use zend http client (with cURL adapter) in zend project to replace the cULR part of old php project. I got stuck-up last 2 days. I would really appreciate help in this regard.
Upvotes: 1
Views: 2201
Reputation: 2447
This is only a rough mockup which I've not run; but try it out:
$api = "demo.api.net/api/";
$config = array(
'adapter' => 'Zend_Http_Client_Adapter_Curl',
'curloptions' => array(
CURLOPT_SSL_VERIFYHOST => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => FALSE
),
);
$client = new Zend_Http_Client($api, $config);
$data = '<?xml version="1.0" encoding="utf-8"?>
<Request version="1.0">
<Export mode="UPDATE" type="COREDATA">
<Login>
<User>user</User>
<Password>password</Password>
</Login>
</Export>
</Request>
';
$client->setConfig(array(
'maxredirects' => 0,
'timeout' => 30)
);
$client->setRawData($data, 'text/xml')->request('POST');
$fh = fopen("va.xml", "w");
fwrite($fh,$client->getLastResponse());
fclose($fh);
Upvotes: 1