Php cUrl不起作用,但回旋镖/邮差工作

时间:2017-03-13 10:09:29

标签: php curl

我和回旋镖合作,但它和邮递员差不多(我想)。 使用Boomerang我可以正常进行通话,但是当我尝试用php做同样的事情时,我不能。

这里有两个标题:

BOOMERANG / POSTMAN

GET https://example.es/api/bla/bloum/174 HTTP/1.1

Authorization: Basic ajklfsdkjfalksjdflñaskjdflakjdf=
Server: Apache-Coyote/1.1
X-Result-MaxResults: 1
X-Result-CurrentPage: 1
X-Result-MaxPages: 1
Content-Type: application/json;charset=UTF-8
Date: Fri, 28 Oct 2011 08:24:22 GMT
Transfer-Encoding: chunked

我的代码

GET /api/bla/bloum/174 HTTP/1.1

Host: example.es
Accept: */*
Authorization: Basic ajklfsdkjfalksjdflñaskjdflakjdf=
Server: Apache-Coyote/1.1
X-Result-MaxResults: 1
X-Result-CurrentPage: 1
X-Result-MaxPages: 1
Content-Type: application/json;charset=UTF-8
Date: Fri, 28 Oct 2011 08:24:22 GMT
Transfer-Encoding: chunked

我不能把这个网址与Boomerang显示的方式相同,我不知道这是不是问题。

POSTMAN CODE

    $curl = curl_init();

    curl_setopt_array($curl, array(
    CURLOPT_URL => "https://example.es/api/bla/bloum/174",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_SSL_VERIFYHOST => false,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "GET",
    CURLOPT_HTTPHEADER => array(
        "authorization: Basic ajklfsdkjfalksjdflñaskjdflakjdf=",
        "cache-control: no-cache",
        "content-type: application/json;charset=UTF-8",
        "date: Fri, 28 Oct 2011 08:24:22 GMT",
        "postman-token: FJKFK",
        "server: Apache-Coyote/1.1",
        "transfer-encoding: chunked",
        "x-result-currentpage: 1",
        "x-result-maxpages: 1",
        "x-result-maxresults: 1"
        ),
    ));

    $response = curl_exec($curl);
    $err = curl_error($curl);

    curl_close($curl);

    if ($err) {
        echo "cURL Error #:" . $err;
    } else {
        echo $response;
    }

更新

$ error = curl_error($ ch); //“从服务器清空回复”

$ httpCode = curl_getinfo($ ch,CURLINFO_HTTP_CODE); // 0

更新2.0

我使用POSTMAN生成代码,现在错误是: “操作在30001毫秒后收到0字节时超时”

无: CURLOPT_TIMEOUT => 30 //“从服务器清空回复”

1 个答案:

答案 0 :(得分:0)

此代码有效:

$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => 'https://example.es/api/asd/zxc/123',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_SSL_VERIFYHOST => false,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'GET',
    CURLOPT_HTTPHEADER => array(
        'authorization: Basic ghfgfiytfgfigfkgfkgjkbvjkhgkjhgkjhg',
        'cache-control: no-cache',
        'content-type: application/json;charset=UTF-8',
        'server: Apache-Coyote/1.1',
        'x-result-currentpage: 3',
        'x-result-maxpages: 1',
        'x-result-maxresults: 1'
    ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  $this->logger->error($err);
}

但我不知道为什么,抱歉。

我用POSTMAN导出它,第二次用它...