无法处理请求" cURL"

时间:2015-08-31 11:53:49

标签: php curl libcurl

我创建了一个使用curl发送xml数据的函数,当我点击该函数然后在我的浏览器上收到一条消息"无法处理请求",不明白代码是否有错,下面是我的函数:

    function otp_data_send($xmlblock='',$url='' ){
   $otp_xml_data = htmlentities($xmlblock);
   $headers = array(
   "Content-type: text/xml",
   "Content-length: " . strlen($otp_xml_data),
   "Connection: close",
    );
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $otp_xml_data);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    $data = var_dump(curl_exec($ch));
    // print_r($data);exit;
    echo $data;
    if(curl_errno($ch))
       print curl_error($ch);
    else
       curl_close($ch);

 }

并在另一个函数中调用此函数。请帮忙

1 个答案:

答案 0 :(得分:1)

尝试此操作以获取更详细的错误

function otp_data_send($xmlblock='',$url='' ){
   $otp_xml_data = htmlentities($xmlblock);
   $headers = array(
   "Content-type: text/xml",
   "Content-length: " . strlen($otp_xml_data),
   "Connection: close",
    );
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $otp_xml_data);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    if(curl_exec($ch) === false) {
        return 'Curl error: [' . curl_errno($ch) . '] ' . curl_error($ch);
    } else {
        return 'Operation completed without any errors';
    }
}

echo otp_data_send(...);

更新1

function otp_data_send($xmlblock='',$url='' ){
   $otp_xml_data = htmlentities($xmlblock);
   $headers = array(
   "Content-type: text/xml",
   "Content-length: " . strlen($otp_xml_data),
   "Connection: close",
    );
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $otp_xml_data);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $output = curl_exec($ch);
    $info = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    return '[' . $info . ']' . $output;
}

echo otp_data_send(...);