简体   繁体   中英

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:

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

Using cURL, I have currently been using :

$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):

GET /testDir/curlPost.php HTTP/1.1

Accept: 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, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393

Accept-Encoding: gzip, deflate

Host: f0946724.ngrok.io

X-Forwarded-For: 92.11.143.199

200 OK HTTP/1.1 200 OK

Date: Thu, 08 Dec 2016 00:48:20 GMT

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

X-Powered-By: PHP/7.0.9

Content-Length: 161

Content-Type: text/html; charset=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

As well as the error stated from 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

Any help much appreciated, thanks.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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