POST调用失败(file_get_contents警告?)

时间:2015-05-22 18:20:47

标签: php rest post

我想使用以下代码进行一次调用:

function addWorkout() {
// url for workouts
$url = "https://jawbone.com/nudge/api/v.1.1/users/@me/workouts";

// set data to send
$data = http_build_query(array(
    'time_created' => intval($_GET['starttime']),
    'time_completed' => intval($_GET['endtime']),
    'sub_type' => intval($_GET['sport']),
    'calories' => intval($_GET['calories']),
));

var_dump($data);

$options = array(
    'http' => array(
        "header" => "Content-Type: Authorization: Bearer {$_COOKIE['access_token']}\r\n",
        'method' => 'POST',
        'content' => $data
    ),
);

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

var_dump($result);

}

然而,它并没有成功发布电话。将显示以下消息:Warning

Warning: file_get_contents(https://...@me/workouts): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in C:\wamp\www\FitnessWebApp\.idea\html\sport.php on line 90
Call Stack
#   Time    Memory  Function    Location
1   0.0006  280568  {main}( )   ..\sport.php:0
2   1.2201  293408  addWorkout( )   ..\sport.php:19
3   1.2204  296272  file_get_contents ( )   ..\sport.php:90

$ data的var_dump:

string 'time_created=1368652731&time_completed=1368656325&sub_type=1&calories=333' (length=73)

$ options的var_dump:

    array (size=1)
  'http' => 
    array (size=3)
      'header' => string 'Content-Type: Authorization: Bearer ...

' (length=166)
      'method' => string 'POST' (length=4)
      'content' => string 'time_created=1368652731&time_completed=1368656325&sub_type=1&calories=333' (length=73)

API文档显示以下示例:

POST https://jawbone.com/nudge/api/v.1.1/users/@me/workouts HTTP/1.1
Host: jawbone.com
time_created=1368652731&time_completed=1368656325&sub_type=3&calories=115

2 个答案:

答案 0 :(得分:0)

文件获取内容不适用于https。你将不得不使用卷曲。

/* gets the data from a URL */
function get_data($url) {
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //will not complain about certs
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}

然后致电

$returned_content = get_data('https://jawbone.com/nudge/api/v.1.1/users/@me/workouts');

答案 1 :(得分:0)

好的,你想用file_get_contents做一个POST请求。

我认为错误就在这一行:

package so17380731; import com.ning.http.client.AsyncHttpClient; import java.nio.charset.StandardCharsets; import java.util.Base64; import javax.ws.rs.core.HttpHeaders; public class BasicAuth { public static void main(String... args) throws Exception { try(AsyncHttpClient asyncClient = new AsyncHttpClient()) { final String user = "StackOverflow"; final String password = "17380731"; final String fetchURL = "https://www.eventick.com.br/api/v1/events/492"; final String encoded = Base64.getEncoder().encodeToString((user + ':' + password).getBytes(StandardCharsets.UTF_8)); final String body = asyncClient .prepareGet(fetchURL) .addHeader(HttpHeaders.AUTHORIZATION, "Basic " + encoded) .execute() .get() .getResponseBody(StandardCharsets.UTF_8.name()); System.out.println(body); } } }

此处有两个元素"header" => "Content-Type: Authorization: Bearer {$_COOKIE['access_token']}\r\n"Content-Type

我建议将其更改为:

Authorization

我不知道$options = array( 'http' => array( "header" => [ "Authorization: Bearer " . $_COOKIE['access_token'], "Content-Type: application/x-www-form-urlencoded"], 'method' => 'POST', 'content' => $data ), ); 是否正确,通常是API使用Content-Type

或者只是离开它。