简体   繁体   中英

Send an HTTP request to a URL using PHP

I am trying to send an HTTP request to an URL using the below code,but keep getting error

(file_get_contents( http://localhost:81/Help/Api/POST-SMS ): failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error)

, any idea? Thank in advance

<?php

$url = 'http://localhost:81/Help/Api/POST-SMS';
 $data = array('message' => 'hi', 'mobile' => '12345678');
$options = array(
'http' => array(
    'header'  => "Content-type: application/json",
    'method'  => 'POST',
    'content' => http_build_query($data)
)
);
 $context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { /* Handle error */ }
var_dump($result);
?>

From the server response "HTTP request failed! HTTP/1.1 500 Internal Server Error" - that means the http://localhost:81/Help/Api/POST-SMS failed - not your actual script that you posted above.

I also recommend using the curl approach in PHP: http://php.net/manual/en/curl.examples-basic.php http://php.net/manual/en/book.curl.php

Another exemple that might help you is this: PHP + curl, HTTP POST sample code?

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