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 ! Link to comment https://forums.phpfreaks.com/topic/220649-context-php-post/ 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[ Link to comment https://forums.phpfreaks.com/topic/220649-context-php-post/#findComment-1142899 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 :/ Link to comment https://forums.phpfreaks.com/topic/220649-context-php-post/#findComment-1142900 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 Link to comment https://forums.phpfreaks.com/topic/220649-context-php-post/#findComment-1142903 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. Link to comment https://forums.phpfreaks.com/topic/220649-context-php-post/#findComment-1142911 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? Link to comment https://forums.phpfreaks.com/topic/220649-context-php-post/#findComment-1142966 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/ Link to comment https://forums.phpfreaks.com/topic/220649-context-php-post/#findComment-1143005 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.