2009-12-29 5 views
1

나는 현재 사이트에 대한 신용 카드 처리를 설정 중입니다. PHP는 CURL 지원으로 컴파일되지 않았으므로 PHP를 다시 컴파일하기 위해 사이트를 다운시키지 않으므로 제공된 예제 코드와 다른 방법을 사용하려고합니다.CURL을 PHP의 fosckopen로 변환하십시오.

예 CURL 코드 :

다른 방법을 이용할 수 있도록 시도

function send($packet, $url) { 
    $header = array("MIME-Version: 1.0","Content-type: application/x-www-form-urlencoded","Contenttransfer-encoding: text"); 
    $ch = curl_init(); 

    // set URL and other appropriate options 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_VERBOSE, 1); 
    curl_setopt ($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); 
    // Uncomment for host with proxy server 
    // curl_setopt ($ch, CURLOPT_PROXY, "http://proxyaddress:port"); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $packet); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt ($ch, CURLOPT_TIMEOUT, 10); 

    // send packet and receive response 
    $response = curl_exec($ch); 
    curl_close($ch); 
    return($response); 
} 
이 실제로 내게 전화하고있는 URL에서 수익을주고 않지만

function send($packet, $host, $path) { 
    $content = ''; 
    $flag = false; 
    $post_query = urlencode($packet) . "\r\n"; 
    $fp = fsockopen($host, '80'); 
    if ($fp) { 
     fputs($fp, "POST $path HTTP/1.0\r\n"); 
     fputs($fp, "Host: $host\r\n"); 
     fputs($fp, "Content-length: ". strlen($post_query) ."\r\n\r\n"); 
     fputs($fp, $post_query); 

     while (!feof($fp)) { 
      $line = fgets($fp, 10240); 
      if ($flag) { 
       $content .= $line; 
      } else { 
       $headers .= $line; 
       if (strlen(trim($line)) == 0) { 
        $flag = true; 
       } 
      } 
     } 
     fclose($fp); 
    } 

    return $content; 
} 

이 그렇게 잘못 때문에 수행 형식이 잘못되었다는 오류가 다시 나타납니다. 나는 CURL이나이 다른 방법에 익숙하지 않아, 내가 뭘 잘못하고 있는지 확신 할 수 없다. 특별히 fsockopen()을 요청했지만

답변

0

당신은 또한 시도 할 수있는 stream api 당신은 내가의 수정 된 버전을 시도하는 대신 file_get_contents()

+0

처럼 전화 해! 그것은 완벽하게 작동했습니다. –

+0

흠, 실제로 이것은 HTTPS에서 작동하지 않습니다. 어떻게 작동합니까? –

+0

오 https. 예 ....하지만 https의 SSL 부분을 제공하는 모듈이 필요합니다. 그리고 그것은 당신이 피하고 싶었던 것입니다. php가 다른 모듈을로드하게하려면 웹 서버를 다시 시작하십시오. PHP의 대부분의 "표준"버전은 http://docs.php.net/openssl에 openssl- 확장자를 사용합니다.하지만 taadaa ... curl 확장자 일 수도 있습니다. 나는 당신이 ssl 전송 계층의 프로덕션 수준의 "순수한"PHP 구현을 발견 할 것이라는 것을 다소 의문 스럽다. 그래도 틀릴 수 있습니다. – VolkerK

1

의 사용 fopen()stream_get_meta_data() 처리 이상의 오류가 필요한 경우

function send($packet, $url) { 
    $ctx = stream_context_create(
    array(
     'http'=>array(
     'header'=>"Content-type: application/x-www-form-urlencoded", 
     'method'=>'POST', 
     'content'=>$packet 
    ) 
    ) 
); 
    return file_get_contents($url, 0, $ctx); 
} 

context options 귀하의 솔루션 - 일단 모든 것이 작동하는 것처럼 보였습니다, 여러 fputs()가 대상 서버에서 돌아 오는 간헐적 인 HTTP 505 오류를 초래한다는 사실을 발견했습니다. 이 문제를 해결하려면

는, 전체 요청을 사전에 구축했고, 당신을 감사 한 fputs이

$post_vars = ""; 

$post_vars .= "&somekey=somevalue"; 

$header = ""; 

$header .= "POST /my.php HTTP/1.0\r\n"; 

$header .= "Content-Type: application/x-www-form-urlencoded\r\n"; 

$header .= "Host: www.myhost.com \r\n"; 

$header .= "Content-Length: " . strlen($post_vars) . "\r\n\r\n"; 

$fp = ""; 

@$fp = fsockopen ('ssl://www.myhost.com', 443, $errno, $errstr,30); 

fputs($fp, $header . $post_vars); 

while(!feof($fp)) { 

    // Read the data returned 

    $response .= @fgets($fp, 4096); 

} 

fclose ($fp); 
관련 문제