php - CURL 无法将 POSTFIELDS 正确发送到某些服务器

标签 php json curl

我尝试过搜索,其他答案对我没有帮助。

我正在尝试通过 cURL 发送 POST 数据,但它仅适用于某些服务器。是什么赋予了?

我已经尝试过:

if (!empty($data) && $usePost) {
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}

$data 包含一个数组,其中包含 products 键和 JSON 字符串值。

我还尝试显式使用查询:

if (!empty($data) && $usePost) {
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
}

但是,接收服务器上的 $_POST 仍然给我 NULL

知道该怎么做吗?

这是整个方法:

public static function execCommand($command, $ch,$data=array(),$cookie_file='genCookie.txt', $usePost = false) {
    $url = $command;
    if (!empty($data) && !$usePost)
        $url .= '?' . http_build_query($data);
    elseif (isset($data['sessionid'])) {
        $url .= '?sessionid=' . $data['sessionid'];
        unset($data['sessionid']);
    }

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:21.0) Gecko/20100101 Firefox/21.0');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

    if (!empty($data) && $usePost) {
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }

    return curl_exec($ch);
}

我尝试回显 $_REQUEST,因此它错误地不在 $_GET 中。我也尝试了 file_get_contents('php://input'),但仍然没有任何结果。

发送端和接收端都在Apache服务器上。

更重要的是,它可以在大多数服务器上运行,但只有一些服务器忽略它。有没有更安全、更跨平台的方法来做到这一点?

最佳答案

我发现很难相信 Curl 没有将“post”字段发送到某些服务器(您共享的代码看起来不错)。我宁愿假设您在这些服务器上处理 POST 的方式有问题,或者您在其他地方做错了。

您可以使用 tcpdump 进行故障排除

$ sudo tcpdump -nl -w - -s0 -A -c 500 tcp port 80 | strings

现在,如果您运行如下示例代码:

<?php

class Curl {

    protected static function getCurlHandler( $url, $data = false, $headers = false ) {
        $ch =  curl_init(); 

        curl_setopt( $ch, CURLOPT_URL, $url );
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt( $ch, CURLOPT_HEADER, 0 );

        if( $headers ) {
            curl_setopt($ch, CURLINFO_HEADER_OUT, true);
            curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
        }

        if( $data ) {
            curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $data ) );
        }

        return $ch;
    }

    protected static function exec( $ch ) {
        $output = curl_exec( $ch );

        // handle errors

        curl_close( $ch );
        return $output;
    }

    public static function get( $url, $headers = array() ) {
        $ch = self::getCurlHandler( $url, false, $headers );

        return self::exec( $ch );
    }

    public static function post( $url, $data = array(), $headers = array() ) {
        $ch = self::getCurlHandler( $url, $data, $headers );
        curl_setopt( $ch, CURLOPT_POST, 1 );

        return self::exec( $ch );
    }

    public static function put( $url, $data = array(), $headers = array() ) {
        $ch = self::getCurlHandler( $url, $data, $headers );
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");

        return self::exec( $ch );
    }

    public static function delete( $url, $data = array(), $headers = array() ) {
        $ch = self::getCurlHandler( $url, $data, $headers );
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");

        return self::exec( $ch );
    }

}


Curl::post( "http://www.google.com", array('q' => 'hello world'));

您应该在 TCPDUMP 输出中找到类似的内容

POST / HTTP/1.1
Host: www.google.com
Accept: */*
Content-Length: 13
Content-Type: application/x-www-form-urlencoded
q=hello+world

最后一行是最有趣的部分,它清楚地表明 CURL 发送了包含所需数据的 POST 请求。

关于php - CURL 无法将 POSTFIELDS 正确发送到某些服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23369964/

相关文章:

php - 忽略 PHP streamcontext/curl/soap 中的弱 SSL 算法

php://输入 <> $_POST?

linux - curl 响应显示 "HTTP version not supported",错误 505

php - Google Cloud API - 应用程序默认凭据

php - {inheritdoc} 的需求是什么?

php - 帮助 PHP 和 HTML 复选框

java - 如何通过 Java 中的一个值获取 JSON 对象?

javascript - 数组位于另一个数组中

java - curl 相当于 -u

phpcurl ssl验证