PHP file get html doesnt return anything - php

include('simple_html_dom.php');
$html = file_get_html("https://www.psacard.com/cert/43696531");
echo $html;
This will not print anything out for me, but I will get it from other websites. Any specific reason why?
file_get_contents also returns nothing.
$url = "https://www.psacard.com/cert/43696531";
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'GET',
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
$string = preg_replace('/\s+/S', " ", $result);
if ($result === FALSE) { /* Handle error */ }
$stringVal = strval($string);
echo $stringVal;

Related

How do I get the result from response of this php api?

I've been working with this api and can't get anything response. Apparently the response should be true or false.
It returns Array;
I've tried things like:
return $obj[0][0];
But the page is blank
Here's the code:
function checkGuest ( $intEvent, $strBarcode, $ourSubscriptionKey, $sessionTokenClient )
{
$body = '';
$opts = array('http' =>
array(
'method' => 'GET',
'header' =>
"Content-Type: application/json\r\n".
"Authorization: Bearer " . $sessionTokenClient ."\r\n".
"Ocp-Apim-Subscription-Key: " . $ourSubscriptionKey,
'content' => $body,
'timeout' => 60
) );
$context = stream_context_create($opts);
$url = 'https://api.qflowhub.io/v1/api/guests/'.$intEvent.'/bybarcode?barcode='.$strBarcode;
try
{
$result = file_get_contents($url, false, $context);
$obj = json_decode( $result );
return $obj;
}
catch (Exception $ex)
{
$result = $ex;
}
}
$userCheck = checkGuest ( $intEvent, $strBarcode, $ourSubscriptionKey, $sessionTokenClient ) ;
echo $userCheck;

parsing json_decode output null

I have tried to parse one variable from json decoded variable, i always got null, i even tried to encode to utf-8.
My code:
<?php
$url = 'https://ots-list.org/ajax/getServers';
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => 'server_name=Return+of+Sayians&game_type=---&client_version=---&server_country=---'
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { /* Handle error */ }
//var_dump($result);
$string = mb_convert_encoding($result, "UTF-8");
$data = json_decode($string);
print $data->{'players_online'};
?>

Error 400 with PATCH method using PHP stream_context_create function to pass JSON data to API

I am trying to post to this Microsoft API but I'm getting an Error 400. Works fine when using their browser-based API test console and in Postman but I can't get it working using my PHP code below. Where am I going wrong?
$myObj->update->name = "API test";
$myObj->update->qnaList->qnaId = "331";
$myObj->update->qnaList->answer = "I have been updated";
$myObj->update->qnaList->source = "Editorial";
$myObj->update->qnaList->questions->add = "What is your name";
$myObj->update->qnaList->metadata->add->name = "category";
$myObj->update->qnaList->metadata->add->value = "personality";
$data = json_encode($myObj);
$url = '[https://apiurl]';
$options = array(
'http' => array(
'method' => 'PATCH',
'content' => $data,
'header' => "Content-Type: application/json\r\n" .
"Ocp-Apim-Subscription-Key: [mykey]"
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
$response = json_decode($result, true);
print_r($response);
There are problems with the JSON body you are trying to send.
This works for me and returns 204:
<?php
$myObj->update->name = "API test";
$myObj->update->qnaList = array();
$myObj->update->qnaList[0]->qnaId = "1";
$myObj->update->qnaList[0]->answer = "I have been updated";
$myObj->update->qnaList[0]->source = "Editorial";
$myObj->update->qnaList[0]->questions->add = array();
$myObj->update->qnaList[0]->questions->add[0] = "What is your name";
$myObj->update->qnaList[0]->metadata->add = array();
$myObj->update->qnaList[0]->metadata->add[0]->name = "category";
$myObj->update->qnaList[0]->metadata->add[0]->value = "personality";
$data = json_encode($myObj);
$kbid = "<YOUR-KB-ID>";
$url = 'https://westus.api.cognitive.microsoft.com/qnamaker/v3.0/knowledgebases/' . $kbid;
$options = array(
'http' => array(
'ignore_errors' => true,
'method' => 'PATCH',
'content' => $data,
'header' => "Content-Type: application/json\r\n" .
"Ocp-Apim-Subscription-Key: <YOUR-KEY>\r\n"
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
$response = json_decode($result, true);
echo $response . "\r\n";
?>

dynamic site in file_get_contents

problem , Crossing the CAPTCHA in dynamic site .. :(
problem link action form in https://edu.uast.ac.ir ..
sorry My English is not good.
please test my code :
my source code :
a.php
<?php
echo file_get_contents("https://edu.uast.ac.ir");
?>
b.php
<?php
$a = $_POST["captcha_first_page"];
$postdata = http_build_query(
array(
'captcha_first_page' => $a
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents('https://edu.uast.ac.ir/user/user_manage/userLogin/', false, $context);
echo $result;
?>
please help me
You have to replace the form action, too, cause if not the submitted form want posted to your b.php.
captcha-a.php
<?php
$content = file_get_contents("https://edu.uast.ac.ir");
$content = str_replace('https://edu.uast.ac.ir/user/user_manage/userLogin/','captcha-b.php', $content);
echo $content;
captcha-b.php
<?php
function redirect_post($url, array $data, array $headers = null) {
$params = array(
'http' => array(
'method' => 'POST',
'content' => http_build_query($data)
)
);
// replace
$headers['Host'] = "edu.uast.ac.ir";
$headers['Origin'] = "https://edu.uast.ac.ir";
$headers['Referer'] = "https://edu.uast.ac.ir";
if (!is_null($headers)) {
$params['http']['header'] = '';
foreach ($headers as $k => $v) {
$params['http']['header'] .= "$k: $v\n";
}
}
$ctx = stream_context_create($params);
$fp = fopen($url, 'rb', false, $ctx);
if ($fp) {
return stream_get_contents($fp);
} else {
// Error
throw new Exception("Error loading '$url', $php_errormsg");
}
}
setlocale(LC_ALL, "en_US.UTF8");
echo redirect_post("https://edu.uast.ac.ir/user/user_manage/userLogin/" ,$_POST, getallheaders());
This should be a good starting point. Don't know if it works cause I can't really speak arabian.

PHP - Posting JSON via file_get_contents

I am trying to POST JSON content to a remote REST endpoint, however the 'content' value appears to be empty on delivery. All other headers etc are being received correctly, and the web service tests successfully with a browser based test client.
Is there a problem with my syntax below where I specify the 'content' field?
$data = array("username" => "duser", "firstname" => "Demo", "surname" => "User", "email" => "example#example.com");
$data_string = json_encode($data);
$result = file_get_contents('http://test.com/api/user/create', null, stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => array('Content-Type: application/json'."\r\n"
. 'Authorization: username:key'."\r\n"
. 'Content-Length: ' . strlen($data_string) . "\r\n"),
'content' => $data_string)
)
));
echo $result;
This is the code I always use and it looks pretty similar (though this is of course for x-www-form-urlencoded).
Perhaps your username:key needs to be base64_encode'd.
function file_post_contents($url, $data, $username = null, $password = null)
{
$postdata = http_build_query($data);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
if($username && $password)
{
$opts['http']['header'] .= ("Authorization: Basic " . base64_encode("$username:$password"));
}
$context = stream_context_create($opts);
return file_get_contents($url, false, $context);
}
The question was about json, why the accepted answer is about x-www-form?
Json has many cool stuff to struggle about, like utf8_encode
function my_utf8_encode(array $in): array
{
foreach ($in as $key => $record) {
if (is_array($record)) {
$in[$key] = my_utf8_encode($record);
} else {
$in[$key] = utf8_encode($record);
}
}
return $in;
}
function file_post_contents(string $url, array $data, string $username = null, string $password = null)
{
$data = my_utf8_encode($data);
$postdata = json_encode($data);
if (is_null($postdata)) {
throw new \Exception('decoding params');
}
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/json',
'content' => $postdata
)
);
if (!is_null($username) && !is_null($password)) {
$opts['http']['header'] .= "Authorization: Basic " . base64_encode("$username:$password");
}
$context = stream_context_create($opts);
try {
$response = file_get_contents($url, false, $context);
} catch (\ErrorException $ex) {
throw new \Exception($ex->getMessage(), $ex->getCode(), $ex->getPrevious());
}
if ($response === false) {
throw new \Exception();
}
return $response;
}
The earlier response of
function file_post_contents($url, $data, $username = null, $password = null) {
$postdata = http_build_query($data);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
if($username && $password)
{
$opts['http']['header'] = ("Authorization: Basic " . base64_encode("$username:$password"));
}
$context = stream_context_create($opts);
return file_get_contents($url, false, $context);}
is incorrect. This function works sometimes, but it is inaccurate and will fail if you're not using the Content-type of application/x-www-form-urlencoded and you pass in a username and password.
It's working for the writer because application/x-www-form-urlencoded is the default Content-type, but his handling of the username and password is overwriting the earlier declaration of content type.
Here is the corrected function:
function file_post_contents($url, $data, $username = null, $password = null){
$postdata = http_build_query($data);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'content' => $postdata
)
);
if($username && $password)
{
$opts['http']['header'] .= ("Authorization: Basic " . base64_encode("$username:$password")); // .= to append to the header array element
}
$context = stream_context_create($opts);
return file_get_contents($url, false, $context);}
Note the line:
$opts['http']['header' .= (dot equals to append to the array element.)

Categories