简体   繁体   English

通过PHP发送cURL请求

[英]Send cURL request via PHP

How do I go about sending this information: 我该如何发送此信息:

// 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);

To the API: 前往API:

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

Using cURL, I have currently been using : 使用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);

Of which I get the results (NGrok): 我得到的结果(NGrok):

GET /testDir/curlPost.php HTTP/1.1 GET /testDir/curlPost.php HTTP / 1.1

Accept: text/html, application/xhtml+xml, image/jxr, / 接受:text / html,application / xhtml + xml,image / jxr, /

Accept-Language: en-GB 接受语言:en-GB

User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393 用户代理: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 接受编码:gzip,放气

Host: f0946724.ngrok.io 主持人:f0946724.ngrok.io

X-Forwarded-For: 92.11.143.199 X-Forwarded-For:92.11.143.199

200 OK HTTP/1.1 200 OK 200 OK HTTP / 1.1 200 OK

Date: Thu, 08 Dec 2016 00:48:20 GMT 日期:2016年12月8日,星期四00:48:20 GMT

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

X-Powered-By: PHP/7.0.9 X-Powered-By:PHP / 7.0.9

Content-Length: 161 内容长度:161

Content-Type: text/html; 内容类型:text / html; charset=UTF-8 字符集= UTF-8

Error: call to URL http://ms.4url.eu/lookup failed with status 0, response , curl_error Failed to connect to ms.4url.eu port 80: Connection refused, curl_errno 7 错误:对URL http://ms.4url.eu/lookup的调用失败,状态为0,响应,curl_error无法连接到ms.4url.eu端口80:连接被拒绝,curl_errno 7

As well as the error stated from cUrl: 以及从cUrl中指出的错误:

Error: call to URL http://ms.4url.eu/lookup failed with status 0, response , curl_error Failed to connect to ms.4url.eu port 80: Connection refused, curl_errno 7 错误:对URL http://ms.4url.eu/lookup的调用失败,状态为0,响应,curl_error无法连接到ms.4url.eu端口80:连接被拒绝,curl_errno 7

Any help much appreciated, thanks. 任何帮助,不胜感激,谢谢。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM