Jump to content

Context - PHP POST


YBthebest

Recommended Posts

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.