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
https://forums.phpfreaks.com/topic/220649-context-php-post/
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
https://forums.phpfreaks.com/topic/220649-context-php-post/#findComment-1142903
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.