简体   繁体   中英

PHP POST API call

Trying to make a POST API call and I get the error file_get_contents( http://api.turfgame.com/v4/users ): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request. API info can be found at http://api.turfgame.com

<?php
$data = array("name" => "DIProgan");

$url = 'http://api.turfgame.com/v4/users';

$options = array(
  'http' => array(
   'method'  => 'POST',
   'content' => json_encode( $data ),
   'header'=> "Content-Type: application/json\r\n" .
               "Accept: application/json\r\n"
   )
);

$context  = stream_context_create( $options );
$result = file_get_contents( $url, false, $context );
$response = json_decode( $result );

var_dump($response);
?>

Using XAMPP & PHP 5.4.4. I tried using "requests for PHP" since I liked the Python version of it but it just complains about not finding hook functions. People seem to hate cURL and from the little I need it seemed easier without it. Maybe not. Help?

It seems like you have just created an array where you have put your parameter. It should be stored within an object first.

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