Controle if posted api link started - php

I have application which post data to another site(running other application) with API link, and now I need returned feedback like "Application started!" or "error".. I tried to control variable $result but It returns me nothing. Application is started and everything works fine if I visit link with posted results.
If I visit API link manually, it just give me blank page(runned).
proces.php
<?php
if(isset($_POST['submit'])) {
$ssId = $_POST['ssId'];
$red = $_POST['red'];
$id = $_POST['id'];
$user = $_POST['user'];
$postdata = http_build_query(
array(
'ssId' => $ssId,
'red' => $red,
'id' => $id,
'user' => $user
)
);
$opts = array('http' =>
array(
'user' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents('https://apiLink.com/someKey&ssId='.$ssId.'&red='.$red.'&id='.$id.'&user='.$user.'', false, $context);
header("Location: startApp.php");
}
?>

Problem is your current code handles request using $_POST however links trigger http 'GET' method instead of 'POST', also you have some additional checks like $_POST['submit'], hence links redirect fails to work with your logic.
If you replace $_POST with $_REQUEST then things should work with links as well provided you send all the relevant parameters along with it. Other option is just give a button enclosed in form with hidden input fields.

Related

POST with file_get_contents in PHP with Captcha

I want to read an external site after submit code with captcha. But using (PHP) file_get_contents the captcha is loaded 2 times, so its wrong.
The Form load an image to solve the captcha and sent via POST to the original site that show the result. Normally I solve the captcha and than, in the new site, I read (with my eyes) the content.
Now I want to let PHP read the content. I always solve first the captcha (in my page) and than send it to the other site and let (php) "file_get_contents" read the content.
HERE MY CODE..
<?php
$captcha = $_POST["captcha"];
?>
<html>
<form method="post" action="(this page)">
<img src="remote-captcha"><input type="text" name="captcha">
<submit>
<?php
$postdata = http_build_query(
array(
'captcha' => $captcha
)
);
$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://www.form-page', false, $context);
$search = '/<div class="myclass">(.*)<\/div>/';
preg_match($search, $result, $show);
echo $show[0];
?>
At this point the form is loaded 2 times so the captcha is wrong.
Can someone help me?

Redirect in php server side after http request

I am very new to php so this question might be trivial.
I am trying to understand if it is possible to redirect the browser from php server side to the page returned by HTTP request.
I have a HTTP Post request looking like so:
$postdata = http_build_query(
array(
"someData" => "data" ,
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'content' => $postdata,
'header' =>
"Cookie: someCookie" .
'content-type: application/x-www-form-urlencoded; charset=utf-8'
)
);
$context = stream_context_create($opts);
$result = fopen('myWebsiteUrl', 'r', false, $context);
var_dump(stream_get_contents($result));
In the post I am being redirected to a different page with Get.
I am trying to force the browser to move to the 'redirected' page. With the above code I am getting back the html of the redirected page but what I'm after is an actual redirect.
The option of retrieving the redirect URL and doing the redirect myself in PHP doesn't work because the Get request has to happen within the same session as the Post.
header('Location: whereveryouwantogo.php?get=123456');
does a redirect for you with a GET-parameter attached. I'm not sure if I really got your question right, but I'm pretty sure you could do what you want/need with the header-command.

how to use another website to make a query with php?

I want to use sci-hub.cc to download scientific papers, but it is in russian and it is not easy to use.
It has a textfield for enter DOI or paper link.
Now, I want to make a website that, when users insert paper link, send this link to sci-hub as a query and then download it. How can I do this in php?
Thanks a lot.
i dont know how your page is working but what you looking for is a "POST" Request with.
The code below "does" the same if you type in something in the field on your page:
<php
$url = 'http://sci-hub.cc/';
$data = ['Request'=>'TEXT FOR FIELD'];
$options = array('http' => array(
'method' => 'POST',
'content' => http_build_query($data),
'header'=> "Content-Type: application/x-www-form-urlencoded \r\n")
);
$context = stream_context_create( $options );
$result = file_get_contents( $url, false, $context );
?>

Receive PHP code with an POST request

I need to send a POST request to another file called global.php, for this I try this code below:
$url = 'global.php';
$data = array('stack' => 'overflow');
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result);
This is the global.php file that should process the request:
if(isset($_POST['stack'])){
echo 'exists';
}else{
echo 'error';
}
The problem is that instead of the command var_dump ($ result); show exists, it shows the PHP code? how can I solve this problem?
And why when I try to do the same thing using ajax it returns me the text exists and not PHP code?
You should use full url, to process php file through server.
$url = 'http://YOURURL.com/global.php';
AJAX call is made from browser, to absolute URL, thats why You are getting desired response.

AuthKey in header

So my client needs a REST API using PHP that provides output as per the conditions on the URL parameters
So now there are three URL's basically which is currently needed and they are done.
so they are
localhost/newapi/client/<AuthKey> - for authorizing
localhost/newapi/client/<clientid>/categories/ - to get all the categories
localhost/newapi/client/<clientid>/categories/<categoryid> - to get all items in a category
used .htaccess for fancy URL
So now he requested that AuthKey need to be added to HTTP header not the URL. So the AuthKey must be passed as header and the rest as URL parameters
So my question is how this can be done. and how to retrieve the AuthKey from the request?
Any tutorials or comments regarding this question is welcome
you can tell the client when he request your api he add a header as below:
AuthKey: your-api-auth-key
or
Token: your-api-token
and then in your php code make
$headers = getallheaders();
$token = $headers['Token'] or ['AuthKey'];
then you check if the key in database and then process your code
Note:
your client can add Header with PHP cURL
curl_setopt($curl-handle, CURLOPT_HEADER, array(
'Token' => 'client-auth-token', //or
'AuthKey' => 'client-auth-token'
));
you can use this code to connect with rest in php
$url = 'localhost/newapi/client/';
$opts = array('http' =>
array(
'method' => 'POST',
'header' => "Content-Type: application/json\r\n"."Authorization: Basic ".<authkey>."\r\n",
'content' => $data,
'timeout' => 60
)
);
$context = stream_context_create($opts);
$result = file_get_contents($url, false, $context, -1, 40000);
return $result;

Categories