cURL适配器在zend框架1.12中使用

时间:2013-12-03 10:13:54

标签: php zend-framework curl adapter

我有一个用普通的php编写的项目,使用curl并且它有效。现在我想把这个项目移到zend框架1(我是zend的新手)。我试图通过Zend_Http_Client_Adapter_Curl进行连接,但是我没有得到足够的信息我需要的东西         我的纯PHP代码:         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);

这里我在查询resquest之后创建va.xml文件。现在,我希望通过使用cURL适配器等效于Zend框架1,有人可以帮忙吗?我曾尝试在zend项目中使用zend http客户端(使用cURL适配器)来替换旧php项目的cULR部分。我最近2天被困了。我非常感谢这方面的帮助。

1 个答案:

答案 0 :(得分:1)

这只是一个粗略的模型,我没有跑;试试吧:

$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);