PHP 中的 cURL 仅部分删除标头


cURL in PHP only partially removing headers

耗尽理智

我看了高看低,失去了一点生命和很多理智,试图返回 cURL POST 请求。一切正常,除了我无法删除标题。哦,是的,我试过了

curl_setopt($ch, CURLOPT_HEADER, false);

更糟糕的是,当我尝试时

$headerLength = curl_getinfo($ch, CURLINFO_HEADER_SIZE);

我得到 25 的回报,这是完全错误的。请帮助我在这个智慧的尽头。

所以我称这种方法

      $service = new RestfulService($Url1);
      $response = $service->request('', 'POST', $tokenXml, null,
array(CURLOPT_SSL_VERIFYPEER => false, 
CURLOPT_ENCODING =>'UTF-8'));

反过来调用(使用 SilverStripe 框架)

public function curlRequest($url, $method, $data = null, $headers = null, $curlOptions = array()) {
    $ch        = curl_init();
    $timeout   = 5;
    $sapphireInfo = new SapphireInfo(); 
    $useragent = 'SilverStripe/' . $sapphireInfo->Version();
    $curlOptions = $curlOptions + (array)$this->config()->default_curl_options;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    if(!ini_get('open_basedir')) curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
    //include headers in the response
    curl_setopt($ch, CURLOPT_HEADER, true);
    // Add headers
    if($this->customHeaders) {
        $headers = array_merge((array)$this->customHeaders, (array)$headers);
    }
    if($headers) curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    // Add authentication
    if($this->authUsername) curl_setopt($ch, CURLOPT_USERPWD, $this->getBasicAuthString());
    // Add fields to POST and PUT requests
    if($method == 'POST') {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    } elseif($method == 'PUT') {
        $put = fopen("php://temp", 'r+');               
        fwrite($put, $data);
        fseek($put, 0); 
        curl_setopt($ch, CURLOPT_PUT, 1);
        curl_setopt($ch, CURLOPT_INFILE, $put);
        curl_setopt($ch, CURLOPT_INFILESIZE, strlen($data)); 
    }
    // Apply proxy settings
    if(is_array($this->proxy)) {
        curl_setopt_array($ch, $this->proxy);
    }
    // Set any custom options passed to the request() function
    curl_setopt_array($ch, $curlOptions);
    // Run request
    $rawResponse = curl_exec($ch);
    if (curl_error($ch))    var_dump(curl_error($ch));

    $response = $this->extractResponse($ch, $rawResponse);
    curl_close($ch);
    return $response;
}

当我转储响应时,删除标头命令仅删除前几个字符。这样。。。

object(RestfulService_Response)[1065]
  protected 'simpleXML' => null
  protected 'cachedResponse' => boolean false
  protected 'statusCode' => int 100
  protected 'statusDescription' => string 'Continue' (length=8)
  protected 'headers' => 
    array (size=0)
      empty
  protected 'body' => string 'ontrol: no-cache
Pragma: no-cache
Content-Length: 3593
Content-Type: application/soap+xml; charset=utf-8
Expires: Fri, 20 Sep 2013 01:14:14 GMT
Server: Microsoft-IIS/7.5
P3P: CP="DSP CUR OTPi IND OTRi ONL FIN"
X-XSS-Protection: 0
PPServer: PPV: 30 H: CO1IDOLGN62 V: 0
Date: Fri, 20 Sep 2013 01:15:13 GMT
Connection: close

�<?xml version="1.0" encoding="utf-8" ?><S:Envelope xmlns:S="http://www.w3.org/2003/05/soap-envel

它与之前的奇怪角色有什么关系吗

更新:我已经在同事的电脑上检查了这个代码,他没有任何问题。我正在运行 WAMP,我的同事正在使用 MAC,是否有任何设置问题?我已经下载了最新的 curl.dll并将其添加到我的 php 库中,但仍然没有雪茄。

有什么想法吗???

就我而言,编码不正确,我需要添加以下选项

curl_setopt($ch, CURLOPT_SSLVERSION, 3)

现在工作正常