Choe s
Choe s

Reputation: 45

Why file_get_contents return error?

I'm working OAuth connect Yahoo.jp login API

I try sending http request use file_get_contents but It's return errors

Here is my code

// my apps setting 
    $client_id = 'dj0zaiZpP~~~~~~~~~~~~~~~';
    $appSecret = '129ad~~~~~~~~~~~~~~~~';

    // the data to send
    $data = array(
        'grant_type' => 'authorization_code',
        'redirect_uri' => 'My_redierct_page', 
        'code' => $_GET['code']

    );
    $data = http_build_query($data);

    $header = array(
        "Authorization: Basic " . base64_encode($client_id . ':' . $appSecret),
        "Content-Type: application/x-www-form-urlencoded",
        "Content-Length: ".strlen($data)
    );

    // build your http request
    $context = stream_context_create(array(
        'http' => array(
            'method' => 'POST', 
            'header' => implode("\r\n", $header),
            'content' => $data, 
            'timeout' => 10
            )
        ));

    // send it
    $resp = file_get_contents('https://auth.login.yahoo.co.jp/yconnect/v1/token', false, $context);
    $json = json_decode($resp);

    echo($json->token_type . " " . $json->access_token);

The result...

file_get_contents(https://auth.login.yahoo.co.jp/yconnect/v1/token): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in /var/www/html/api/auth_proc2.php on line 33


Here is another error message get using set_error_handler()

file_get_contents(): Content-type not specified assuming application/x-www-form-urlencoded

I can't understand this situation
Because I send Content-type in http header and allow_url_fopen = on in my php.ini

Please help me~! Thanks.

Upvotes: 0

Views: 753

Answers (2)

Jeff Wilbert
Jeff Wilbert

Reputation: 4520

The other thing I'd suggest using is CURL rather then file_get_contents for multiple reasons; first you'll have a lot more control over the request, second its more standard to use curl requests when dealing with API's, and third you'll be able to see better what your problem is.

Try replacing your code with the following and see what you get.

$client_id = 'dj0zaiZpP~~~~~~~~~~~~~~~';
$appSecret = '129ad~~~~~~~~~~~~~~~~';

$data = array(
    'grant_type' => 'authorization_code',
    'redirect_uri' => 'My_redierct_page', 
    'code' => $_GET['code']
);

$curl = curl_init('https://auth.login.yahoo.co.jp/yconnect/v1/token'); 

curl_setopt($curl, CURLOPT_POST, true); 
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($curl, CURLOPT_USERPWD, $client_id . ':' . $appSecret);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 

$response = curl_exec($curl);
$info = curl_getinfo($curl);

print_r($response);
echo '<br /><br />';
print_r($info);

curl_close($curl);

Upvotes: 1

Jeff Wilbert
Jeff Wilbert

Reputation: 4520

I assume its probably because your using Content-Type and not Content-type and Content-Length instead of Content-length.

Upvotes: 0

Related Questions