【问题标题】:Send cURL request via PHP通过 PHP 发送 cURL 请求
【发布时间】:2017-04-23 04:09:20
【问题描述】:

我该如何发送这些信息:

// 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, /

接受语言:en-GB

用户代理: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

接受编码: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

服务器:Apache/2.4.23 (Win32) OpenSSL/1.0.2h PHP/7.0.9

X-Powered-By:PHP/7.0.9

内容长度:161

内容类型:文本/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

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

【问题讨论】:

  • 首先,`ms.4url.eu/lookup` 是否可以访问..?因为我从未见过 http 状态码 0。
  • 是的,对 Json 格式数据的 http 请求返回 POST /testDir/getPost.php HTTP/1.1 主机:1f89e4a8.ngrok.io 接受:应用程序/json 内容类型:应用程序/json 内容-长度:198 连接:关闭 X-Forwarded-For:5.44.233.221 {"id":"ea26d0b2-b839-46b9-9138-50cc791bab47","msisdn":"44712​​3121234","status":"Success"," networkCode":"23471","countryName":"UK","countryCode":"GBR","network":"O2","networkType":"GSM","ported":"No"}。跨度>
  • 呃不是生成的请求,而是 ms.4url.eu/lookup .. 因为你的curl error is no 7, that means unable to connect,并且有人曾经在 stackoverflow 中问过such case .. 你可以尝试调用 curl 来另一个域,例如 google - 如果它也失败,请检查您的防火墙。

标签: php html json curl request


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-21
    • 1970-01-01
    • 2017-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-16
    • 2014-07-28
    相关资源
    最近更新 更多