1

我正在编写一些代码来连接到 Neteller API。他们的 API 使用 POST 请求来接受参数并以 XML 形式返回。我想通过 PHP 连接到 API,所以我认为 CURL 请求应该能够发布参数并解析响应。不幸的是,当我尝试请求时,我得到了一个 HTTP 404,但如果我使用一个简单的 html 表单,它就可以了。

HTML 表单

<form method="post" runat="server" Action="https://test.api.neteller.com/instantpayout">
     <input type="text" name="version" value="4.0" />
     <input type="text" name="amount" size="10" maxlength="10" />
     <input type="hidden" name="merchant_id" value="1234567890" />
     <input type="hidden" name="merch_pass" value="mypass" />
     <input type="hidden" name="merch_account" value="john123" />
     <input type="hidden" name="merch_key" value="Your Merchant Key" />          
     <input type="text" name="net_account" size="20" maxlength="12">    
     <input type="hidden" name="custom_1" value="test123" maxlength="50">
     <input type="hidden" name="custom_2" value="test123" maxlength="50">
     <input type="hidden" name="custom_3" value="test123" maxlength="50">      
     <input type="hidden" name="merch_transid" value="91919191" maxlength="50">        
     <input type="hidden" name="currency" size="10" value='EUR' maxlength="3">                               
     <input type="submit" name=”button” value="Make Transfer">
</form>

PHP 代码

if ($this->isSandbox()) {
    $url = 'https://test.api.neteller.com/instantpayout';
} else {
    $url = 'https://api.neteller.com/instantpayout';
}   

$fields = array(
    'version' => $version,
    'amount' => urlencode($amount),
    'merchant_id' => urlencode($merchant_id),
    'merch_pass' => urlencode($merchant_password),
    'merch_account' => urlencode($merchant_account),
    'merch_key' => urlencode($merchant_key),
    'net_account' => urlencode($net_account),
    'custom_1' => urlencode($custom_1),
    'custom_2' => urlencode($custom_2),
    'custom_3' => urlencode($custom_3),
    'merch_transid' => urlencode($merchant_transid),
    'currency' => $currency,
    'button' => 'Make Transfer'
);

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; pl; rv:1.9) Gecko/2008052906 Firefox/3.0");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_CAINFO, APP . "Lib" . DS . "curl" . DS . "cacert.pem");

if($this->isSandbox()){
    curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
}

$output = curl_exec($ch);
$info = curl_getinfo($ch);

if($output === false){
    $error = curl_error($ch);
    pr($error);
} else {
    $response = simplexml_load_string($output);
    pr($response);
}

卷曲信息

Array
(
    [url] => https://test.api.neteller.com/instantpayout
    [content_type] => text/plain; charset=UTF-8
    [http_code] => 404
    [header_size] => 192
    [request_size] => 299
    [filetime] => -1
    [ssl_verify_result] => 18
    [redirect_count] => 0
    [total_time] => 1.076
    [namelookup_time] => 0
    [connect_time] => 0.187
    [pretransfer_time] => 0.686
    [size_upload] => 1386
    [size_download] => 0
    [speed_download] => 0
    [speed_upload] => 1288
    [download_content_length] => -1
    [upload_content_length] => 1386
    [starttransfer_time] => 0.889
    [redirect_time] => 0
    [certinfo] => Array
        (
        )

    [redirect_url] => 
)

我尝试了几种 CURL 选项,但似乎没有任何效果。

谢谢

4

1 回答 1

5

当您使用数组 curl设置CURLOPT_POSTFIELDS发送 multipart/form-data 请求时,您需要将其设置为字符串以发送 application/x-www-form-urlencoded 请求。将该行更改为:

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
于 2012-11-05T10:06:48.640 回答