PHP Classes

Yahoo PUT request

Recommend this page to a friend!

      PHP OAuth Library  >  PHP OAuth Library package blog  >  How to Implement PHP ...  >  All threads  >  Yahoo PUT request  >  (Un) Subscribe thread alerts  
Subject:Yahoo PUT request
Summary:How to successfully implement a yahoo PUT request
Messages:9
Author:Kevin
Date:2014-05-18 20:05:28
Update:2014-05-21 17:29:57
 

  1. Yahoo PUT request   Reply   Report abuse  
Picture of Kevin Kevin - 2014-05-18 20:05:28
Hi,

I have another question that I'm having trouble with. I am able to successfully access a user's private data, and now I want to make some changes to their account. The data I'm looking to modify only supports GET and PUT methods. Now I've noticed that only GET and POST have been implemented, but from what I've gathered, a POST/PUT request should be structured the same?

With that being said, I was hoping you could help me understand if what I said is correct, and if so, how I can create a proper PUT request to update data for a user.

Now, I'm using this as a resource to structure my content that I'm trying to send back to yahoo (https://developer.yahoo.com/fantasysports/guide/roster-resource.html#roster-resource-PUT)

Here is where I want to send my request:

fantasysports.yahooapis.com/fantasy ...

And here is the data that I want to send

<?xml version="1.0"?>
<fantasy_content>
<roster>
<coverage_type>date</coverage_type>
<date>2011-05-01</date>

<players>
<player>
<player_key>253.p.8332</player_key>
<position>1B</position>
</player>
<player>
<player_key>253.p.1423</player_key>
<position>BN</position>
</player>
</players>
</roster>
</fantasy_content>

  2. Re: Yahoo PUT request   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2014-05-19 00:17:44 - In reply to message 1 from Kevin
The CallAPI function supports any HTTP method including PUT.

Use the RequestBody parameter to pass the XML request data and set the RequestContentType parameter to 'text/xml' to tell the server you are sending XML data.

  3. Re: Yahoo PUT request   Reply   Report abuse  
Picture of Kevin Kevin - 2014-05-20 02:21:07 - In reply to message 2 from Manuel Lemos
I'm having problems getting the PUT operation to function properly.

This is what I have set up:

$success = $client->CallAPI(
'http://fantasysports.yahooapis.com/fantasy/v2/team/328.l.203329.t.12/roster',
'PUT',array('format'=>'xml'),
array('FailOnAccessError'=>true,
'RequestBody' => $xmlrequest,
'RequestContentType' => 'text/xml'),
$user);

where $xml = <?xml version="1.0"?>
<fantasy_content>
<roster>
<coverage_type>date</coverage_type>
<date>2014-05-20</date>
<players>
<player>
<player_key>328.p.7907</player_key>
<position>OF</position>
</player>
</players>
</roster>
</fantasy_content>

However I keep getting the following error:

Error: it was not possible to access the API call: it was returned an unexpected response status 401 Response: <?xml version='1.0' encoding='UTF-8'?> <yahoo:error xmlns:yahoo='http://yahooapis.com/v1/base.rng' xml:lang='en-US'> <yahoo:description>Please provide valid credentials. OAuth oauth_problem="unable_to_determine_oauth_type", realm="yahooapis.com"</yahoo:description> </yahoo:error> <!-- fan1333.sports.bf1.yahoo.com uncompressed/chunked Mon May 19 19:15:29 PDT 2014 -->

Other than what I posted, I have not changed any of the code from the yahoo sample. I know I'm logging into yahoo with the correct credentials to access the data I'm trying to modify. Is my PUT request setup properly?

  4. Re: Yahoo PUT request   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2014-05-20 02:55:43 - In reply to message 3 from Kevin
It seems you are not passing a valid either client id, client secret, or the access token. Are you calling CallAPI after the Process function?

  5. Re: Yahoo PUT request   Reply   Report abuse  
Picture of Kevin Kevin - 2014-05-20 07:07:13 - In reply to message 4 from Manuel Lemos
Yes. I am able to call the following without a problem:

$success = $client->CallAPI(
'http://fantasysports.yahooapis.com/fantasy/v2/team/328.l.203329.t.12/roster;date=2014-05-20',
'GET', array(
'format'=>'xml'
), array('FailOnAccessError'=>true), $user);

It just seems when I go to use PUT I'm missing something.

  6. Re: Yahoo PUT request   Reply   Report abuse  
Picture of Kevin Kevin - 2014-05-20 15:49:21 - In reply to message 5 from Kevin
Here is the entire file

<?php
/*
* login_with_yahoo.php
*
* @(#) $Id: login_with_yahoo.php,v 1.4 2014/03/31 07:18:57 mlemos Exp $
*
*/

/*
* Get the http.php file from http://www.phpclasses.org/httpclient
*/
require('http.php');
require('oauth_client.php');

$client = new oauth_client_class;
$client->debug = false;
$client->debug_http = true;
$client->server = 'Yahoo';
$client->redirect_uri = 'http://'.$_SERVER['HTTP_HOST'].
dirname(strtok($_SERVER['REQUEST_URI'],'?')).'/login_with_yahoo.php';

$client->client_id = 'XXXXXXXXXXXXXXXXXXXX'; $application_line = __LINE__;
$client->client_secret = 'XXXXXXXXXXXXXXXXXXXX';

$xmlrequest = '<?xml version="1.0"?>
<fantasy_content>
<roster>
<coverage_type>date</coverage_type>
<date>2014-05-20</date>
<players>
<player>
<player_key>328.p.7907</player_key>
<position>OF</position>
</player>
</players>
</roster>
</fantasy_content>';


if(strlen($client->client_id) == 0
|| strlen($client->client_secret) == 0)
die('Please go to Yahoo Apps page https://developer.apps.yahoo.com/projects/ , '.
'create a project, and in the line '.$application_line.
' set the client_id to Consumer key and client_secret with Consumer secret. '.
'The Callback URL must be '.$client->redirect_uri).' Make sure you enable the '.
'necessary permissions to execute the API calls your application needs.';

if(($success = $client->Initialize()))
{
if(($success = $client->Process()))
{
if(strlen($client->access_token))
{
$success = $client->CallAPI(
'http://fantasysports.yahooapis.com/fantasy/v2/team/328.l.203329.t.12/roster;date=2014-05-20',
'GET', array(
'format'=>'xml'
), array('FailOnAccessError'=>true), $user);
$success = $client->CallAPI(
'http://fantasysports.yahooapis.com/fantasy/v2/team/328.l.203329.t.12/roster',
'PUT',array('format'=>'xml'),array('FailOnAccessError'=>true, 'RequestBody' => $xmlrequest, 'RequestContentType' => 'text/xml'), $user);
}
}
$success = $client->Finalize($success);
}
if($client->exit)
exit;
if(strlen($client->authorization_error))
{
$client->error = $client->authorization_error;
$success = false;
}
if($success)
{
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Yahoo OAuth client results</title>
</head>
<body>
<?php
echo '<h1>', HtmlSpecialChars($user->query->results->profile->nickname),
' you have logged in successfully with Yahoo!</h1>';
echo '<pre>', HtmlSpecialChars(print_r($user, 1)), '</pre>';
?>
</body>
</html>
<?php
}
else
{
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>OAuth client error</title>
</head>
<body>
<h1>OAuth client error</h1>
<p>Error: <?php echo HtmlSpecialChars($client->error); ?></p>
</body>
</html>
<?php
}

?>

  7. Re: Yahoo PUT request   Reply   Report abuse  
Picture of Kevin Kevin - 2014-05-21 00:44:18 - In reply to message 6 from Kevin
Do you see anything that might be causing the problem? Or could you suggest a good way to debug this?

It works fine with the GET request, so I feel like my credentials should be fine. It has to be something with how I set it up.

  8. Re: Yahoo PUT request   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2014-05-21 06:58:24 - In reply to message 5 from Kevin
Yes, there was a bug in the logic to encode request parameters for non-POST requests. Then bug was fixed and a new version was uploaded. Just let me know if you still have problems.

  9. Re: Yahoo PUT request   Reply   Report abuse  
Picture of Kevin Kevin - 2014-05-21 17:29:58 - In reply to message 8 from Manuel Lemos
Okay great thanks so much for the help. I got it working smoothly now!