passing variable to another page when it is called using curl_setopt - php

I am usig curl_setopt to eval another file from a different server, the link is something like this "http://somewhere.com/index.php?vars=hello"
now in somewhere.com/index.php i need to get the value of vars that was passed using curl, but so far i cant get any values at all.
here is my sample code for your reference this is from the file calling somewhere.com:
$d = "http://somewhere.com/index.php?vars=hello";
$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch1, CURLOPT_URL, $d);
curl_setopt($ch1, CURLOPT_FOLLOWLOCATION, TRUE);
$data1 = curl_exec($ch1);
eval($data1);
curl_close($ch1);
in somewhere.com/index.php i already did print_r($_GET); to view any passed values to the file but it returned nothing.

What is happening in the index.php file with the variable "hello"? When you do a curl post to another page that other page typically will echo out a response and that response is what you evaluate. Can you post your code from index.php so we can see what you are trying to do?
Edit: Also you can use chrome inspector, fiddler or some other network monitor to see if the http request actually is being fired off and to check that you are actually getting a 200 response back.
Edit: I don't know what you are using eval either, just echo the response. If you have an output buffer issue then start buffering before you echo the response like:
ob_start():
echo $data1;
ob_end_clean();
Also if you want to see if you are getting any errors just do this:
if(curl_exec($ch1) === false)
{
echo 'Curl error: ' . curl_error($ch1);
}
else
{
echo $data1;
}
// Close handle
curl_close($ch1);

can't you just use $_GET[]?
i.e.
$variables = $_GET['vars'];

Related

PHP & CURL scraping

I have a problem when I run this script in Google Chrome I got a blank page. When I use another link of a web site, it works successfully. I do not what is happening.
$curl = curl_init();
$url = "https://www.danmurphys.com.au/dm/home";
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($curl);
echo $output;
There are some conditions which make your result blank. Such as:
Curl error.
Redirection without response body and the curl doesn't follow the redirection.
The target host doesn't give any response body.
So here you have to find out the problem.
For the first possibility, use curl_error and curl_errno to confirm that the curl wasn't errored when its runtime.
For the second, use CURLOPT_FOLLOWLOCATION option to make sure the curl follows the redirection.
For the third possibility, we can use curl_getinfo. It returns an array which contains "size_download". The size_download shows you the length of the response body. If it is zero that is why you see a blank page when printing it.
One more, try to use var_dump to see the output (debug purpose only). There is a possibility where the curl_exec returns bool false or null. If you print the bool false or null it will show a blank.
Here is the example to use all of them.
<?php
$curl = curl_init();
$url = "https://www.danmurphys.com.au/dm/home";
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
$output = curl_exec($curl);
$info = curl_getinfo($curl);
$err = curl_error($curl);
$ern = curl_errno($curl);
if ($ern) {
printf("An error occurred: (%d) %s\n", $ern, $err);
exit(1);
}
curl_close($curl);
printf("Response body size: %d\n", $info["size_download"]);
// Debug only.
// var_dump($output);
echo $output;
Hope this can help you.
Update:
You can use CURLOPT_VERBOSE to see the request and response information in details.
Just add this
curl_setopt($curl, CURLOPT_VERBOSE, true);
It doesn't need to be printed, the curl will print it for you during runtime.

PHP - get the returned value with CURL between two sites

I have two PHP sites, an API site (x.php) and another, which calls the API site with CURL (y.php)
The x.php looks like this:
if (isset($_POST['testconnection'])) {
return "ok";
}
And the y.php like this:
$host = "https://there.is.the/x.php";
$command = array (
"testconnection" => "testconnection"
);
$ch=curl_init($host);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($command));
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT,180);
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
As you can see in the example, I would like to get the return string from x.php in the y.php, but I get an empty string in the answer: string(0) ""
I think it would be: string(2) "ok"
I have replaced the return to echo in x.php but without success.
Sorry if it is a noob question, I'm quite new in curl.
Replace
return "ok";
With
echo "ok";
Your script is exiting with return value, but nothing is output as a response.
A thing to note is that web server must have permissions to access the x.php. Verify that the script is able to execute. Permission problems often result in a blank page.
Tip: Use Postman to test REST APIs. You could shoot the post query against your x.php and see what comes back, eliminating the curl part being wrong.

cURL not working in PHP?

I have the following php function:
function checkJQL($filter) {
$auth = "account:password";
$URL='http://jira/rest/api/latest/search?jql='.$filter;
echo $URL;
// Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//Tell it to always go to the link
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$URL);
//Set username and password
curl_setopt($ch, CURLOPT_USERPWD, $auth);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);
echo $result;
// Will dump a beauty json :3
var_dump(json_decode($result, true));
echo "DONE";
}
When I call this with $filter = "" It works fine, outputs everything. As soon as I insert a proper JQL query, it fails. When I enter random garbage, it works (As in I get a invalid input message), but when it's proper JQL it never works.
When I copy and paste the URL that I echo into the browser it works.
An example filter I used:
"Target" = "Blah"
When I think about it, I don't actually need this to work, I just need it to know when the input isn't JQL (which it does). But I'm really curious now. Anyone have ideas on what it might be?
You should URL-encode the $filter.
The reason why it works with empty or random strings is because they don't have any challenging characters that need to be URL-encoded.
The reason why the URL works in the browser but not in the script is because the browser does the URL encoding.
So do the following:
$URL='http://jira/rest/api/latest/search?jql='.urlencode($filter);
Link to urlencode: http://php.net/manual/en/function.urlencode.php

curl_exec() outputting destination contents to page?

I'm using the following code:
$ch = curl_init('www.google.com');
$output = curl_exec($ch);
curl_close($ch);
This is not the first time I've used cURL, and unless I'm mistaken the above code should retrieve the contents of google.com and store it in $output. Correct?
So, why then, does the above code output the contents (in this example the Google homepage) to the page? I'm not echo'ing anything out, but for some reason the curl_exec() function is outputting what it returns to the page.
Am I missing something?
you need to use
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
That will tell curl_exec not to output the results
so change it all to
$ch = curl_init('www.google.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);

Post and get result with PHP

From a PHP page, I'm trying to POST some data to another PHP page and get data back.
Here is what I currently have:
<?php
// Initialize cURL session
$ch = curl_init('postpage.php');
// Set some options on the session
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('field1' => 'Andrew'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute post
$result = curl_exec($ch);
var_dump($result);
// Close the session
curl_close($ch);
?>
and in postpage.php:
<?php
echo 'Receipt of post request. field1:'.$_POST["field1"];
?>
All the var_dump gives me is this:
string(0) ""
Why am I not getting the text back?
Thanks!
The curl executed from your php script is not aware of the current environment, that is, you cannot use relative urls. The url you supply to curl_init must be absolute (i.e. including http://)
If this is indeed the actual initialization you're doing, the error may lie in the arguments of curl_init() which expects a fully qualified URL.
Also, you might want to employ some error diagnostics. curl_exec returns FALSE on failure. The reason why can be determined with curl_error():
$ch = curl_init( ... );
// ... some curl_setopt()
$result = curl_exec($ch);
if ($result === FALSE)
{
die("cURL error: " . curl_error($ch));
}

Categories