I'm hoping someone can help me out. I inherited some code that I have no idea how it works and now that it broke I have to fix it.
I am running PHP 5.5.38 on Windows server 2012 and IIS 8.5. I have finally discovered that the php code is using httprequest which doesn't exist anymore in my version of php and with windows. How would I convert the following to use curl code, if it is even possible?
$http = new HttpRequest('https://my.securelink.com/external/readi/ssoRequest', HttpRequest::METH_POST);
$http->addHeaders(
array(
'READI_AccessKey' => $accesskey,
'READI_Timestamp' => $timestamp,
'READI_RequestSignature' => $mac
)
);
$http->addPostFields(
array(
'READIUsername' => 'myusername',
'LastName' => $_GET["lastn"],
'FirstName' => $_GET["firstn"],
'Email' => $_GET["em"],
'ForceNewAssessment' => false,
'InternalID' => $_GET["userid"],
'IncludeSettings' => ''
)
);
//Optional Post Field: 'READIUserID' => 'xxxxxx'
$response = $http->send()->getBody();
echo "<h3>===== Response ===== </h3>";
echo "<pre>";
echo $response;
echo "</pre>";
$arr = xml2array($response);
$rtn_status = getvaluebypath($arr,'sso/status');
$rtn_timestamp = getvaluebypath($arr,'sso/timestamp');
if($rtn_status == "success") {
$redirectURL = getvaluebypath($arr,'sso/redirectUrl');
echo "<h3>===== Access Link ===== </h3>";
echo "" . $redirectURL . "";
header( "Location: $redirectURL");
}
You can try adding the HttpRequest class as guyver4mk suggested (the link from Jiri Hrazdil's comment) if you don't want to change the whole code.
But since you asked how to convert the existing to cURL, try this:
$header = [
'READI_AccessKey: '.$accesskey,
'READI_Timestamp: '.$timestamp,
'READI_RequestSignature: '.$mac
];
$post = [
'READIUsername' => 'myusername',
'LastName' => $_GET["lastn"],
'FirstName' => $_GET["firstn"],
'Email' => $_GET["em"],
'ForceNewAssessment' => false,
'InternalID' => $_GET["userid"],
'IncludeSettings' => ''
];
$ch = curl_init();
$options = [
CURLOPT_URL => 'https://my.securelink.com/external/readi/ssoRequest',
CURLOPT_HTTPHEADER => $header,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $post,
CURLOPT_RETURNTRANSFER => 1
];
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
if (!$response)
print_r(curl_error($ch));
curl_close($ch);
Related
I'm trying to fetch subtitles from OpenSubtitles (http://trac.opensubtitles.org/projects/opensubtitles/wiki/XMLRPC) like this:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
//Opensubtitles listing
function data($request){
$context = stream_context_create(array('http' => array(
'method' => "POST",
'header' => "Content-Type: text/xml\r\nUser-Agent: PHPRPC/1.0\r\n",
'content' => $request
)));
$server = 'http://api.opensubtitles.org/xml-rpc'; // api url
$file = file_get_contents($server, false, $context);
$response = xmlrpc_decode($file);
return $response;
}
//Get token
$request = xmlrpc_encode_request("LogIn", array('', '', 'eng', 'TemporaryUserAgent'));
$token = data($request)['token'];
//Get listing
$request = xmlrpc_encode_request("SearchSubtitles", array(
'imdb' => '0462499',
'sublanguageid' => 'eng',
'season' => '',
'episode' => '',
'token' => $token
));
$response = data($request);
var_dump($response);
?>
However I keep getting 401 Unauthorized. Does anyone know how to fix this problem? I know it's not a problem with the API because I am able to retrieve the token just fine.
Try using your username/password instead empty string.
And change UserAgent in TemporaryUserAgent in Header as written in
http://trac.opensubtitles.org/projects/opensubtitles/wiki/DevReadFirst
The second request should be in the following format:-
$request = xmlrpc_encode_request("SearchSubtitles", array($token, array(array('sublanguageid' => 'eng', 'imdbid' => 'your_imdbid'))));
Hope this helps.
In my wp project, I am using Assently for e-signature implementation. Though I have an account and created a pdf form file to be filled by the user I just am not able to proceed a bit. I am finding documentation not clear.
Also, I am not clear about what needs to be done so that the user will be shown form to process the transaction.
So, any help/suggestions to proceed forward is appreciated.
I have tried the following based on assently-laravel. But it's asking me to login. What is an error here?
Code:
define('ASSENTLY_DEBUG', true);
define('ASSENTLY_KEY', 'key');
define('ASSENTLY_SECRET', 'secret-generated');
include_once('assently/Assently.php');
$assently = new Assently();
$assently->authenticate(ASSENTLY_KEY, ASSENTLY_SECRET);
$url = 'https://test.assently.com/api/v2/createcasefromtemplate';
$default = array(
'Id' => '5a0e0869-' . rand(1111, 9999) . '-4b79-' . rand(1111, 9999) . '-466ea5cca5ce'
);
$data = array(
'auth' => $assently->auth(),
'templateId' => '0e004e2b-b192-4ce2-8f47-d7a4576d7df6',
'newCaseId' => '5a0e0869-' . rand(1111, 9999) . '-4b79-' . rand(1111, 9999) . '-466ea5cca5ce',
'agentUsername' => ''
);
$data = array(
'json' => $data
);
$options = array(
'http' => array(
'header' => "Content-type: application/json; charset=utf-8\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
echo '<pre>';
print_r($result);
die;
create this class inside assently folder
use Assently\AssentlyCase;
use Exception;
class CustomAssentlyCase extends AssentlyCase
{
public function createFromTemplate($data)
{
$default = [
'newCaseId' => '5a0e0869-'.rand(1111, 9999).'-4b79-'.rand(1111, 9999).'-466ea5cca5ce'
];
$json = array_merge($default, $data);
try{
$response = $this->client->post($this->url('createcasefromtemplate'), [
'auth' => $this->assently->auth(),
'json' => $json
]);
}catch(Exception $e){
print_r($e->getMessage());
}
return $response;
}
}
Use
define('ASSENTLY_DEBUG', true);
define('ASSENTLY_KEY', 'key');
define('ASSENTLY_SECRET', 'secret-generated');
include_once('assently/Assently.php');
include_once('assently/CustomAssentlyCase.php');
$assently = new Assently();
$assently->authenticate(ASSENTLY_KEY, ASSENTLY_SECRET);
$data = array(
'templateId' => '0e004e2b-b192-4ce2-8f47-d7a4576d7df6',
'newCaseId' => '5a0e0869-'.rand(1111, 9999).'-4b79-'.rand(1111, 9999).'-466ea5cca5ce',
'agentUsername' => 'agentUsername' // PUT your agent username here it is required
);
$customAssentlyCase = new CustomAssentlyCase($assently);
$result = $customAssentlyCase->createFromTemplate($data);
print_r($result);
Try this, though not tested but should work. Good luck.
I've got an issue for laravel 5.4 when I trying to using guzzleHttp. here is my code.
use GuzzleHttp\Client;
$url = 'http://example.com';
$client = new Client();
$parameter = ['query' => ['name' => 'xxx', 'address' => 'yyy'], 'headers' => [ 'User-Agent' => 'xxxx', 'exceptions' => false, 'timeout' => 10 ]];
$res = $client->request('GET', $url, $parameter);
if ($res->getStatusCode() == 200)
{
$json = (string)$res->getBody();
return $json;
}
and I've got this error on log:
Error Exception: Object of class GuzzleHttp\Psr7\Request could not be converted to string
what is wrong with my code? please kindly help me.
fyi, this error not always happen. sometimes it show this error, sometimes success.
thank you
$json = $res->getBody()->getContents();
try this
Try this.....
try {
$parameter = ['query' => ['name' => 'xxx', 'address' => 'yyy'], 'headers' => [ 'User-Agent' => 'xxxx', 'exceptions' => false, 'timeout' => 10 ]];
$res = $client->request('GET', $url, $parameter);
if ($res->getStatusCode() == 200)
{
return $res->getBody()->getContents();
}
}catch(Exception $e){
echo 'Caught exception: ', $e->getMessage();
}
$response = $client->post('http:yanjye.com3', ['phone' => '00','password' => '5555',]);
if ($response->getStatusCode() == 200){
$json = (string)$response->getBody();
return $json;
}
var_dump( $response);
die();
Hello, brother, I think this is the Good way to which is Working On both laravel 5.2[larave 5.2]
i have removed Httm
[laraver 5.2][1]
and use this code :
[1]: https://laravel.com/docs/7.x/http-client`
Where did I make a mistake? My PHP-script:
<?php
// Set username and password
$lgname = "someUsername";
$lgpassword = "somePassword";
// First login to receive 1) token, 2) sessionid and 3) cookieprefix
$parameters = array('action' => 'login', 'lgname' => "$lgname", 'lgpassword' => "$lgpassword", 'format' => 'json');
options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($parameters)
),
);
$context = stream_context_create($options);
$result = file_get_contents("http://en.wikipedia.org/w/api.php", false, $context);
// Echo out the answer from MediaWiki-API
echo "$result";
// Put the needed parts of the answer into variables and echo them out
$array = json_decode($result,true);
$token = $array["login"]["token"];
$sessionid = $array["login"]["sessionid"];
$cookieprefix = $array["login"]["cookieprefix"];
echo "</BR>token: $token, sessionid: $sessionid, cookieprefix: $cookieprefix</BR>";
// Second login to 1) post token and 2) send sessionID within the header
$parameters = array('action' => 'login', 'lgname' => "$lgname", 'lgpassword' => "$lgpassword", 'lgtoken' => "$token", 'format' => 'json');
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n" .
"Cookie: " . $cookieprefix . "_session = $sessionid\r\n",
'method' => 'POST',
'content' => http_build_query($parameters)
),
);
$context = stream_context_create($options);
$result = file_get_contents("http://en.wikipedia.org/w/api.php", false, $context);
// Echo out result
echo "$result";
?>
What I get as an answer to my second POST-request is (exactly the same as to my first POST-request) that I need a token (even though I posted the token and even the sessionID in my second POST-request):
{"login": {
"result":"NeedToken",
"token":"82b3f2e1f1aa702ca6ceae473bb16bde",
"cookieprefix":"dewiki",
"sessionid":"531143bd7425722bf1be88e520dea6d5"}
}
The mistake is in using file_get_contents() in the first place. Use a PHP library for the MediaWiki web API, instead.
If you really want to do things yourself, ask a token from meta=tokens.
i ve been playing with the paypal api for exprescheckout.
the problem: i can not call the L_PAYMENTREQUEST_n_ methods.
the PAYMENTREQUEST_0_ works. but i can use this only for price or itemprice.
for the description and quantity i need to use L_PAYMENTREQUEST_n_ methods.
thx for any help
<?php
// EXpress Checkout Paypal
$paketdata = array(
array(
"name" => "Premium",
"desc" => "Mein Beschreibung",
"preis" => '9.96',
"count" => '2'
)
);
$endpreis = "18.97";
$desc = 'meine beschreibung name';
$paypal ="#";
$user = "paypalap#email.com";
$password = "passwd1234";
$signature ="fhhasHDJKHSAHJAL74327327dbsasahbsdcsadb7434";
$version = "93";
$currency = "EUR";
$params = array(
'METHOD' => 'SetExpressCheckout',
'USER' => $user,
'SIGNATURE' => $signature,
'PWD' => $password,
'RETURNURL' => 'https://localhost/env/paypal/geschafft.php',
'CANCELURL' => 'https://localhost/env/paypal/cancel.php',
'VERSION' => $version,
'PAYMENTREQUEST_0_AMT' => $endpreis,
'PAYMENTREQUEST_0_CURRENCYCODE' => $currency,
);
foreach ($paketdata as $k => $paket){
$params['L_PAYMENTREQUEST_0_NAME$k'] = $paket['name'];
$params['L_PAYMENTREQUEST_0_DESC$k'] = '';
$params['L_PAYMENTREQUEST_0_ITEMAMT$k'] = $paket['preis'];
}
$params = http_build_query($params);
$endpoint = 'https://api-3t.sandbox.paypal.com/nvp';
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $endpoint,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $params,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_VERBOSE => 1
));
$response = curl_exec($curl);
$responseArray = array();
parse_str($response, $responseArray);
if (curl_errno($curl)) {
var_dump(curl_errno($curl));
curl_close($curl);
die();
}else{
if ($responseArray['ACK'] == 'Success') {
}else{
curl_close($curl);
}
}
curl_close($curl);
$paypal = 'https://www.sandbox.paypal.com/webscr?cmd=_express-checkout&useraction=commit&token=' . $responseArray['TOKEN'];
echo '<pre>';
print_r($paket);
echo '</pre>';
print_r($paypal)
?>
PAyPal
Your request is coming out like this...
Array
(
[METHOD] => SetExpressCheckout
[USER] => paypalap#email.com
[SIGNATURE] => fhhasHDJKHSAHJAL74327327dbsasahbsdcsadb7434
[PWD] => passwd1234
[RETURNURL] => https://localhost/env/paypal/geschafft.php
[CANCELURL] => https://localhost/env/paypal/cancel.php
[VERSION] => 93
[PAYMENTREQUEST_0_AMT] => 18.97
[PAYMENTREQUEST_0_CURRENCYCODE] => EUR
[L_PAYMENTREQUEST_0_NAME$k] => Premium
[L_PAYMENTREQUEST_0_DESC$k] =>
[L_PAYMENTREQUEST_0_ITEMAMT$k] => 9.96
)
You need to use double quotes around the param name in order to use $k directly inside like that, so you need this...
foreach ($paketdata as $k => $paket){
$params["L_PAYMENTREQUEST_0_NAME$k"] = $paket['name'];
$params["L_PAYMENTREQUEST_0_DESC$k"] = '';
$params["L_PAYMENTREQUEST_0_ITEMAMT$k"] = $paket['preis'];
}
That change then gives me this, which should go through just fine.
Array
(
[METHOD] => SetExpressCheckout
[USER] => paypalap#email.com
[SIGNATURE] => fhhasHDJKHSAHJAL74327327dbsasahbsdcsadb7434
[PWD] => passwd1234
[RETURNURL] => https://localhost/env/paypal/geschafft.php
[CANCELURL] => https://localhost/env/paypal/cancel.php
[VERSION] => 93
[PAYMENTREQUEST_0_AMT] => 18.97
[PAYMENTREQUEST_0_CURRENCYCODE] => EUR
[L_PAYMENTREQUEST_0_NAME0] => Premium
[L_PAYMENTREQUEST_0_DESC0] =>
[L_PAYMENTREQUEST_0_ITEMAMT0] => 9.96
)
On another note, though, you don't want to use localhost in your return and cancel URL's. PayPal's server is the one redirecting to that URL, so localhost at that time is their own server, which isn't going to do what you want, of course.
You need to use your public IP address or setup a domain that resolves to your IP that you can use.
In fact, it's funny, I just tried to use http:// on the front of localhost here, and Stack won't let me post links with that because of that very reason...they would be linking to themselves.