PHP CURL提供HTTP 404但它在浏览器中工作正常

时间:2012-11-05 09:54:18

标签: php curl

我正在编写一些代码来连接到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);
}

CURL INFO

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选项,但似乎没什么用。

由于

1 个答案:

答案 0 :(得分:5)

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

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));