首页 文章

通过PHP发送cURL请求

提问于
浏览
0

我该如何发送这些信息:

// build an associative array
$data["username"]="a";
$data["password"]="b";
$data["msisdn"]="447123121234";
$data["webhook"]="http://1f89e4a8.ngrok.io/testDir/getPost.php";

// turn it into json formatted string
$json_data=json_encode($data);

到API:

$url = "http://ms.4url.eu/lookup";

使用cURL,我目前一直在使用:

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER,
        array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $json_data);

$json_response = curl_exec($curl);

$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

if ( $status != 201 ) {
    die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
}


curl_close($curl);

其中我得到了结果(NGrok):

GET /testDir/curlPost.php HTTP / 1.1接受:text / html,application / xhtml xml,image / jxr,/ Accept-Language:en-GB User-Agent:Mozilla / 5.0(Windows NT 10.0; Win64; x64) AppleWebKit / 537.36(KHTML,与Gecko一样)Chrome / 51.0.2704.79 Safari / 537.36 Edge / 14.14393 Accept-Encoding:gzip,deflate主机:f0946724.ngrok.io X-Forwarded-For:92.11.143.199 200 OK HTTP / 1.1 200 OK日期:星期四,2016年12月8日00:48:20 GMT服务器:Apache / 2.4.23(Win32)OpenSSL / 1.0.2h PHP / 7.0.9 X-Powered-By:PHP / 7.0.9内容长度:161内容-Type:text / html; charset = UTF-8错误:调用URL http://ms.4url.eu/lookup失败,状态为0,响应,curl_error无法连接到ms.4url.eu端口80:连接被拒绝,curl_errno 7

以及cUrl中指出的错误:

错误:调用URL http://ms.4url.eu/lookup失败,状态为0,响应,curl_error无法连接到ms.4url.eu端口80:连接被拒绝,curl_errno 7

任何帮助非常感谢,谢谢 .

1 回答

  • 0

    我发现我得到的错误是由于服务器期望Https(443)连接而不是Http(80),这很容易通过将URL更改为https://ms.4url.eu而不是http://ms.4url.eu(当使用cURL发送请求时)来解决 .

相关问题