I am making a simple PHP rest service, I am calling this service with CURL here is the code for this
//client code example
$ch = curl_init($URL);
//curl_setopt($ch, CURLOPT_MUTE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
Now on Rest Service I am receiving the request and doing the task. Then I have to send the response back.
//server code example
$xml_post = file_get_contents('php://input');
$xmlparser = new XMLParser;
$parsedata = $xmlparser->parse($xml_post);
$resultobj = new ResultGenerate;
$result = $resultobj->generate($parsedata);
I have no idea how to send the reponse ($result) back.So that $output has the xml string in the end. Please Help
echo $result
is all you need. think about it like this. when using PHP to serve web pages, all you are doing is serving a response to the web browser. You use echo there just like you are using echo here.
Related
I am connecting my two apps (laravel with python) using curl command. It is working absolutely fine on my local server without changing a single line but when it gives me following error when on azure server
POST https://myweb.com/searchJobs 500 ()
my code is
$ch = curl_init();
//URL to send the request to
curl_setopt($ch, CURLOPT_URL, 'http://python-scrapper-python001.azurewebsites.net/myfunction');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
//We are doing a post request
curl_setopt($ch, CURLOPT_POST, 1);
//adding the post data to request
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
//Return instead of outputting directly
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
is there any security protocol that I need to update on azure or what can be possible issue?
Actually these lines were working fine on localhost but not on azure
$arr = "";
$arr['hello'] = '7';
so I changed them as
$arr = [];
$arr['hello'] = '7';
Though i don't no why azure didn't run it. If php convert it to an array on localhost than it must run on azure as well
Since you are using the default domain provided by azure appservice. Instead of just using the HTTP protocol try to use the HTTPS since this is already free on the default domain provided. in this case try this changes.
$ch = curl_init();
//URL to send the request to
curl_setopt($ch, CURLOPT_URL, 'https://python-scrapper-python001.azurewebsites.net/myfunction');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
//We are doing a post request
curl_setopt($ch, CURLOPT_POST, 1);
//adding the post data to request
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
//Return instead of outputting directly
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
I use cURL but untill now I used it for requesting data from servers. But now I want ot write API and data will be requested with cURL. But I don't know how Server reads data from cURL request.
This is my "client server" side request:
function sendRequest($site_name,$send_xml,$header_type=array('Content-Type: text/xml'))
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$site_name);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$send_xml);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPHEADER,$header_type);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
$result = curl_exec($ch);
return $result;
}
$xml = "<request>
<session>
<user>exampleuser</user>
<pass>examplepass</pass>
</session>
</request>";
$sendreq = sendRequest("http://sitename.com/example.php",$xml);
echo $sendreq;
How do I need to write "main server" side script so I can read what user and pass from request are???
Thank you a lot.
To just be able to read it try this
curl_setopt($ch, CURLOPT_POSTFIELDS,array('data'=>$send_xml));
Then
print_r($_POST['data'])
Alternatively skip the XML and try something like this:
$data = array(
'request' => array(
'session' => array(
'user'=>'exampleuser',
'pass'=>'examplepass')
)
);
$sendreq = sendRequest("http://sitename.com/example.php",$data);
In example.php
print_r($_POST)
I have a Affiliate URL Like http://track.abc.com/?affid=1234
open this link will go to http://www.abc.com
now i want to execute the http://track.abc.com/?affid=1234 Using CURL
and now how i can Get http://www.abc.com
with Curl ?
If you want cURL to follow redirect headers from the responses it receives, you need to set that option with:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
You may also want to limit the number of redirects it follows using:
curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
So you'd using something similar to this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://track.abc.com/?affid=1234");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$data = curl_exec($ch);
Edit: Question wasn't exactly clear but from the comment below, if you want to get the redirect location, you need to get the headers from cURL and parse them for the Location header:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://track.abc.com/?affid=1234");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, true);
$data = curl_exec($ch);
This will give you the headers returned by the server in $data, simply parse through them to get the location header and you'll get your result. This question shows you how to do that.
I wrote a function that will extract any header from a cURL header response.
function getHeader($headerString, $key) {
preg_match('#\s\b' . $key . '\b:\s.*\s#', $headerString, $header);
return substr($header[0], strlen($key) + 3, -2);
}
In this case, you're looking for the value of the header Location. I tested the function by retrieving headers from a TinyURL, that redirects to http://google.se, using cURL.
$url = "http://tinyurl.com/dtrkv";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
$location = getHeader($data, 'Location');
var_dump($location);
Output from the var_dump.
string(16) "http://google.se"
I'm using a remote API with cURL and querystrings. The issue I'm having is that I can't see the data being returned to me (which is in XML format). To start, here's my code:
$url = 'https://api.somehost.com/GetRequest.do';
$query = 'User=JohnDoe&Password=ABC123&Function=auth';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$curlresponse = curl_exec($ch);
I know the cURL request is working because if I change the password to something incorrect, I get a response back (can see it using print_r($curlresponse) telling me the password is incorrect.
However, when I enter the correct password, I see nothing when I print_r($curlresponse) yet I know I'm receiving the data because a print_r(curl_getinfo($ch)) shows a larghe download_content_length which is the data I'm expecting. The data I'm expecting back is in XML format. I'm just not sure what I'm missing.
Thank you!
I am trying to write a simple program in PHP to use telegram api, (not bot api, main messaging api). when i run my script, result is 404 error. Why?
<?php
$postfields = array();
$postfields['phone_number'] ='+1234567890';
$postfields['sms_type'] ='0';
$postfields['api_id'] ='12345';
$postfields['api_hash'] ='abcdefghijklmnopqrstuvwxyz';
$postfields['lang_code'] ='en';
$url = '149.154.167.40:443';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
echo $html=curl_exec($ch);
curl_close($ch);
?>
tanks a lot.