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 |
|
![]() 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>
![]() 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.
![]() 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?
![]() 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?
![]() 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.
![]() 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 } ?>
![]() 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.
![]() 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.
![]() Okay great thanks so much for the help. I got it working smoothly now!
|
info at phpclasses dot org
.