Receive data from external php script? - php

please could someone help me?
I have made a php script on my site, that will receive get data, and then return either true or false based on that get data.
I need a way to call that script through php on a completely different server, and then save the returned value.
How would I go about doing this?
Also, how would I go about doing it with POST as well?

You can use cURL to call script on other server and also post data to that script.
//set POST variables
$url = 'http://example.com/post.php';
$fields = array(
'data1' => urlencode($data1),
'data2' => urlencode($data2),
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
More details you will get here: http://php.net/manual/en/ref.curl.php

Something like this will do it for GET:
$response = file_get_contents('your_url_here');
And for POST, I'd use cURL.

Related

Login to another site with CURL POST (PHP)

There is a website provided by our client that displays data I need for a report.
My client refused to build an API so I can retrieve the data I need as JSON, or even better provide read-only access to their DB.
So I thought, I can create a script that runs in a sever, that goes to the site, log in, search for an HTML table tag and retrieve data as an array in a loop, right?
Well, I do not know how to properly log in to the site using cURL.
The login form is as simple as two text boxes and a submit button, the form points to itself and once submitted redirects to another PHP file in the same server, that actually displays the data I need.
I search through the web and I did find a lot of great examples.
But when I try it, it returns a Boolean "True", and that is it.
$url = 'https://www.potato.site/login/login_file.php';
$fields = array(
'username' => urlencode('user'),
'password' => urlencode('password'),
'submit' => urlencode('Login')
);
$fields_string ="";
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
echo $result;
I'm probably missing something here, I will greatly appreciate any help.
Thank you.
You need to set the CURLOPT_RETURNTRANSFER option to get the contents of your request in $result. Otherwise, curl returns true/false to indicate success.
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
// $result now contains page contents

Pass JSON object from php to php

I have a php file (sample1.php) which calls another php file (sample2.php)
through header function.
header("Location: "sample2.php");
How can I pass a JSON object from one php to another.
I need to POST the JSON object, so that the second file can access it using
if( isset($_POST["json"]) ) { ... }
You can use cURL method
<?php
//set POST variables
$url = 'sample2.php';
$fields = array(
'json' => urlencode($json/*your json that want to pass*/),
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
?>
Or you can refer this link :)
If you absolutely have to use header() to proceed to sample2.php then you can't use $_POST to get the data when in sample2.php.
Put it in session storage (in sample1.php):
$_SESSION['jsonData'] = $yourJsonData;
Then after arriving in sample2.php:
if( isset($_SESSION["jsonData"]) ) { ... }
Don't forget to initialize your sessions, etc. Refer to http://php.net/manual/en/session.examples.basic.php

Using Curl in CodeIgniter : How to send data using post method

Not going to succeed when trying to send data using post method. Never used it before.
This is code of executing curl:
$url = LICENSE_URL."validate_system_key/validate_key/";
//url-ify the data for the POST
$data['system_key']='A8Z0-X1N7-S1V2-Y1I5';
$data['domain']='http://example.com';
$fields_string = '';
foreach($data as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
$fields_string = rtrim($fields_string,'&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($data));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,10); # timeout after 10 seconds, you can increase it
//curl_setopt($ch,CURLOPT_HEADER,false);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); # Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
$result=json_decode($result,TRUE);
And this is the Code On Client Url:
public function validate_key(){
$system_key=$_POST['system_key'];
$domain=$_POST['domain'];
$result['error']=3;
echo json_encode($result);
}
Note: When I am not using POST method (sending data through url) evrything is fine. Its all about POST method, not getting the mistake! Any kind of help will be appreciated! Thanks.
Use array instead of String in data passed to string like below:
foreach($data as $key=>$value) {
$data[$key] = $value;
}
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
I hope this will work.

making post requests by curl

hi i wrote this script to get the html of a website but i see "Invalid Request" Error, but when i create a html form and submit it i have no problem, whats the problem? here is my php code :
<?php
$url = 'http://convert2mp3.net/en/index.php?p=convert';
$fields = array(
'url' => urlencode("www.youtube.com/watch?v=ntSBKPkk4m4"),
'format' => urlencode("3gp")
);
//url-ify the data for the POST
$fields_string="";
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_AUTOREFERER,true);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
?>
The value of CURLOPT_POST should be a boolean, not the int you're setting it as.
Try this, if $fields can be dynamic, or just set it to boolean true:
curl_setopt($ch,CURLOPT_POST, count($fields) > 0);
Also, why are you building the POST fields yourself? Just pass the whole array to CURLOPT_POSTFIELDS:
$fields = array(
'url' => "www.youtube.com/watch?v=ntSBKPkk4m4",
'format' => "3gp"
);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields);
You wont success with your missions because they relate source ip address to the key of the conversation that they provide, so after you will get the direct download link in their cdn, your client's won't be able to download it because the related ip address is your web server's ip.

How to send XML and other post parameters via cURL in PHP

I've used code below to send XML to my REST API. $xml_string_data contains proper XML, and it is passed well to mypi.php:
//set POST variables
$url = 'http://www.server.cu/mypi.php';
$fields = array(
'data'=>urlencode($xml_string_data)
);
//url-ify the data for the POST
$fields_string = "";
foreach($fields as $key=>$value)
{
$fields_string .= $key.'='.$value.'&';
}
rtrim($fields_string,'&');
echo $fields_string;
//open connection
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch,CURLOPT_HTTPHEADER,array (
"Expect: "
));
//execute post
$result = #curl_exec($ch);
But when I've added other field:
$fields = array(
'method' => "methodGoPay",
'data'=>urlencode($xml_string_data)
);
It stopped to work. On the mypi.php I don't recieve any more POST parameters at all!
Could you you please tell me what to do to send XML and other post parameters in one cURL request?
Please don't suggest using any libraries, I wan't to acomplish it in plain PHP.
I don't see anything wrong with this script. It's most likely an issue with mypi.php.
You do have an extra & at the end. Maybe that confuses the server? The rtrim doesn't change the $field_string and it returns the trimmed string.
The postfields can be simplified like this,
$fields = array(
'method' => "methodGoPay",
'data'=> $xml_string_data // No encode here
);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));

Categories