YBthebest Posted December 4, 2010 Share Posted December 4, 2010 Hello! This is my code: $postdata = http_build_query( array( 'version' => '1.0', 'username' => 'YBthebest', 'password' => 'MYPASSWORD' ) ); $username = 'YBthebest'; $opts = array( 'http'=>array( 'method'=>"POST", 'header'=>"User-Agent: APIClient:YBthebest\r\n" ) ); $context = stream_context_create($opts); $result = file_get_contents('https://ocrterminal.com/api/user.cgi', false, $context); Warning: file_get_contents(https://ocrterminal.com/api/user.cgi): failed to open stream: HTTP request failed! I get that error! What is failing? And yes, I need to send my username through User_agent !( // User Agent string userAgent = "APIClient:" + username;, taken from C example) Thanks ! Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted December 4, 2010 Share Posted December 4, 2010 your opts reference http. maybe change that to https $opts = array( 'https'=>array( 'method'=>"POST", 'header'=>"User-Agent: APIClient:YBthebest\r\n" ) ); [/code[ Quote Link to comment Share on other sites More sharing options...
YBthebest Posted December 4, 2010 Author Share Posted December 4, 2010 Hey! Tried it, still the same error :/ Quote Link to comment Share on other sites More sharing options...
YBthebest Posted December 4, 2010 Author Share Posted December 4, 2010 using System.Net; using System.Collections.Specialized; ... ... /// <summary> /// Logging in to the OCR Terminal server /// </summary> /// <param name="username">Username (User must be ‘allowed’ to access the API)</param> /// <param name="password">Password</param> public static void login(string username, string password) { // URL of login page string uriString = "https://www.ocrterminal.com/api/user.cgi"; // User Agent string userAgent = "APIClient:" + username; // Create a new WebClient instance WebClient myWebClient = new WebClient(); myWebClient.Headers.Add(HttpRequestHeader.UserAgent, userAgent); // New NameValueCollection instance to hold custom parameters NameValueCollection myNameValueCollection = new NameValueCollection(); // Add necessary parameter/value pairs to the name/value container myNameValueCollection.Add("version", "1.0"); myNameValueCollection.Add("username", username); myNameValueCollection.Add("password", password); // Upload the NameValueCollection and get result byte[] responseArray = myWebClient.UploadValues(uriString, myNameValueCollection); // Decode and record the response string sLoginResult = Encoding.ASCII.GetString(responseArray); if (sLoginResult.IndexOf("User exists") > 0) { // Login was successful; process result // ... } else { // Login was unsuccessful; handle failure // ... } } I don't know if this can help, but this is the basis code. It is an API and they only give an example with C#, so I tried to ''translate'' it to PHP! maybe it can help! Thanks Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted December 4, 2010 Share Posted December 4, 2010 i haven't tried to use file_get_contents with such 'complexity'. i suggest that you look into php curl functions. Quote Link to comment Share on other sites More sharing options...
YBthebest Posted December 4, 2010 Author Share Posted December 4, 2010 Hey ! Well, I really don't know how to use cURL that...complex... Do you have any examples? Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted December 4, 2010 Share Posted December 4, 2010 They have been knocking it around over here: http://www.phpfreaks.com/forums/php-coding-help/how-to-login-to-a-site-with-php/ Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.