HTTP Request file get contents not working - php

I'm trying to send a file through rest using HTTP request to BonitaBPM but the file im trying to send comes out empty when i use file get contents and the HTTP request doesn't work obviously due to that
$file_contents = file_get_contents("C:/inetpub/wwwroot/upload/Director.png");
$data1 = array(
"caseId"=> $case_id[1],
"file"=>"C:/inetpub/wwwroot/upload/Director.png",
"name"=>"doc_invoice",
"fileName"=> $_FILES['file_attach']['name'],
"description"=> "Invoice"
);
//Structure of process data to start case
$options1 = array(
"http" => array(
"method" => "POST",
"header"=> "POST /bonita/API/bpm/caseDocument HTTP/1.1\r\n".
"Host: bonita.libertypr.com\r\n".
"Cookie: ". $display[1]."\r\n".
"Content-Type: application/json\r\n" .
"Accept: application/json\r\n".
"Cache-Control: no-cache\r\n".
"Pragma: no-cache\r\n".
"Connection: Keep-Alive\r\n\r\n",
"content" => json_encode($data1)
)
);
//decode process data and adds document to case
$url1 = "http://bonita.libertypr.com:8081/bonita/API/bpm/caseDocument";
$context1 = stream_context_create($options1);
$result1 = file_get_contents($url1, false, $context1);
$response1 = json_decode($result1);

Please make sure that you are authenticated on the Bonita side before calling this API call.
See this link for more details:
http://documentation.bonitasoft.com/rest-api-overview#authentication
If you are not, the Bonita API calls will be rejected.
To analyze a bit further what is causing the issue, you should get your hands on the HTTP request and response sent between your code and Bonita.
To do so, you can capture the HTTP traffic with a tool such as Wireshark
Cheers,

Related

Sending the form data from my website to a remote http server via php

hello all
i am receiving the http post request from html form to my action.php file and then this php file is writing these values into a text file.
now i want the php file to send the data it receives to a remote http server for further processing (i am using a simple python http server)
here is my php code :
<?php
$data1 = $_REQUEST['key1'];
$data2 = $_REQUEST['key2'];
$data3 = $_REQUEST['key3'];
$data4 = $_REQUEST['key4'];
$data5 = $_REQUEST['key5'];
$fp = fopen('datafile.txt', 'w+');
fwrite($fp, implode("\n", [$data1, $data2, $data3, $data4, $data5]));
fclose($fp);
// example data
$data = array(
'key1'=> $data1,
'key2'=> $data2,
'key3'=> $data3,
'key4'=> $data4,
'key5'=> $data5
);
// build post body
$body = http_build_query($data); // foo=bar&baz=boom
// options, headers and body for the request
$opts = array(
'http'=>array(
'method'=>"POST",
'header'=>"Accept-language: en\r\n",
'data' => $body
)
);
// create request context
$context = stream_context_create($opts);
// do request
$response = file_get_contents('http://2.22.212.12:42221', false, $context)
?>
but when i submit the form , only the datafile.txt file is generated and no post request is sent to the remote python server
what am i doing wrong ?
I would highly recommend using Guzzle, but the docs for stream_context_create use fopen() instead of file_get_contents(), so that might one factor.
Another is the header. The comments on the doc page set the header this way for POST requests:
'header'=> "Content-type: application/x-www-form-urlencoded\r\n"
. "Content-Length: " . strlen($body) . "\r\n",
Take a look at the answers here too for more examples.

file_get_contents with header: Send current header or current cookie in magento or common

I want to call an URL and want to get the result with PHP by using file_get_contents (I know CURL, but first I want to try it with file_get_contents). In my case it's a request to the magento shop system, which requires a previously done login to the backend.
If I execute the URL manually in my browser, the right page is coming. If I send the URL with file_get_contents, I will also get logged in (because I added the Cookie to the request), but everytime I get only the dashboard home site, maybe something causes a redirect.
I tried to simulate the same http request, as my browser send it away. My question is: Is there a possiblity to send the same header data (Cookie, Session-ID etc.) directly as parameter to file_get_contents without manual serialization?
It's a common PHP question, the basic script would be:
$postdata = http_build_query(
array(
'var1' => 'some content',
'var2' => 'doh'
)
);
$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('http://example.com/submit.php', false, $context);
And in my case the code is:
$postdata = http_build_query(
array
(
'selected_products' => 'some content',
)
);
$opts = array('http' =>
array
(
'method' => 'POST',
'header' => "Content-type: application/x-www-form-urlencoded; charset=UTF-8\r\n".
"Cookie: __utma=".Mage::getModel('core/cookie')->get("__utma").";".
"__utmz=".Mage::getModel('core/cookie')->get("__utmz").
" __utmc=".Mage::getModel('core/cookie')->get("__utmc").';'.
"adminhtml=".Mage::getModel('core/cookie')->get("adminhtml")."\r\n".
"X-Requested-With: XMLHttpRequest\r\n".
"Connection: keep-alive\r\n".
"Accept: text/javascript, text/html, application/xml, text/xml, */*\r\n".
"User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0",
'content' => $postdata
)
);
$context = stream_context_create($opts);
var_dump(file_get_contents($runStopAndRemoveProducts, false, $context ));
The result should be the same error message I'll get in the browser by calling the URL manually ("please select some products" as plain text), but the response is a full dashboard home page as html website.
I'm looking for a script like this. I want to make sure all parameters are set automatically without manual build the cookie string and the other ones :)
file_get_contents('http://example.com/submit.php', false, $_SESSION["Current_Header"]);
EDIT: I've found the mistake, two special get-Parameter (isAjax=1 and form_key = Mage::getSingleton('core/session', array('name' => 'adminhtml'))->getFormKey()) are required. In my case the form_key causes the error. But the ugly Cookie string is already there - still looking for a more pretty solution.
To me this looks like you are trying to write a hack for something that you can do more elegantly, the proper, fully documented way. Please have a look at the Magento API.
If you want to delete products (or do anything else):
http://www.magentocommerce.com/api/soap/catalog/catalogProduct/catalog_product.delete.html
You will get a proper response back to know if things have been successful. If there are things the API cannot do then you can extend/hack it if you wish.
To get started you will need an API user/pass and get up to speed with SOAP. The examples in the Magento documentation should suffice. Good luck!

Can't send data in post request via file_get_contents

Need to request some code two times in my app. Fist request url as ajax call, and also need to request this url in controller (something like hmvc). I know how to develop this via curl but I found another kind of idea how to implement this, just use function file_get_contents with before prepared params. This my code:
// Setup limit per page
$args['offset'] = $offset;
$args['limit'] = $this->_perpage;
// --
// Convert search arguments to the uri format
$data = http_build_query($args);
// Define request params
$options = array(
'http' => array(
'header' => 'Content-type: application/json' . PHP_EOL .
'Content-Length: ' . strlen($data) . PHP_EOL,
'method' => 'POST',
'content' => $data,
),
);
$context = stream_context_create($options);
$result = file_get_contents(
'http://'.$_SERVER['HTTP_HOST'].'/search/items', FALSE, $context
);
Request method was detected ok in requested uri, but params wasn't passed. Why this is not pass arguments to request? Where is bug in my code? Many thanks for any answers.
http_build_query builds application/x-www-form-urlencoded content. (not application/json)
There is a full example:
How to post data in PHP using file_get_contents?
Content type should be application/x-www-form-urlencoded. If you want to stay with application/json, try to get posted data using file_get_contents("php://input").

Api in PHP - Accept a curl request and process

Not sure if anyone can help me out with a question.
I had to write some php for the company I work for that lets us integrate with an API that accepts a JSON body. I used the cUrl method, and the script is working great.
If I wanted to build another php page that would accept the request im sending, how would I go about this?
Say I wanted to allow someone to send this same request to me, and then wanted the info they sent to go into my database, how would turn their request into php strings?
Here is the code im sending.
<?
$json_string = json_encode(array("FirstName" => $name, "MiddleName" => " ", "LastName" => $last));;
// echo $json_string;
// jSON URL which should be requested
$json_url = 'http://www.exampleurl.com';
// jSON String for request
// Initializing curl
$ch = curl_init( $json_url );
// Configuring curl options
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array(
'Accept: application/json;charset=utf-8',
'Content-Type: application/json;charset=utf-8',
'Expect: 100-continue',
'Connection: Keep-Alive') ,
CURLOPT_POSTFIELDS => $json_string
);
// Setting curl options
curl_setopt_array( $ch, $options );
// Getting results
$result = curl_exec($ch); // Getting jSON result string
echo $result;
$myArray = json_decode($result);
$action = $myArray->Action;
?>
To get the raw data from the POST that you would be receiving you would use $postData = file_get_contents('php://input');
http://php.net/manual/en/reserved.variables.post.php
Then you would json_decode() the contents of that POST back into JSON.
http://php.net/manual/en/function.json-decode.php
Not really good understood your question. May be you are looking for the way to read raw POST data? In that case open and read from php://stdin stream.
$stdin = fopen('php://stdin', 'r');
By the way read here ( http://php.net/manual/en/function.curl-setopt.php ) how to use CURLOPT_POSTFIELDS. This parameter can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value.

php http request

$post_data = array(
'url' => $all[2],
'op' => 'sv',
'sid' => 1
);
// Send a request to example.com
$result = post_request('http://www.yahoo.com', $post_data);
function PostRequest($url) {
$opts = array('http' =>
array(
'method' => 'GET',
'header' => "Content-type: application/x-www-form-urlencoded\r\n"."Accept-language: en\r\n" .
"Cookie: member_id=8593099\r\n" .
"Cookie: pass_hash=fad917fe75e1059f85fc6d9bb6f7a19f\r\n".
"Cookie: session_id=279fe56fd87e5371dc7e1c9f66c27522"
)
);
$context = stream_context_create($opts);
$result = file_get_contents($url, false, $context);
return $result;
}
I am able to send the request, but my action needs login to be performed.
Even once I'm logged in, it classifies me as not logged in.
I'm using localhost to send out the request. Is that because of the different domain?
I already copied the login cookies for my localhost, but it is still not working.
Any ideas?
What I tried to do is send http request with php.
My request has sent out, but my destination cannot detect cookies, and claim I am not login.
I'm not too sure what you're trying to accomplish, as the code you've posted isn't quite clear. post_request() isn't a native PHP-function, so you'd have to give us a sample of it for us to be able to help you further.
I would however recommend that you've check that you've put session_start(); way up top in your PHP-files - it ensures that you're able to access the session/cookie.

Categories