I am trying to do some POST with CURL in some way, I explain:
I am using the PhpExcel library, the idea is to upload an excel file that contains a table with several records and the names of the field, with a PhpExcel function I go through each record, so I save it in an array.
The next step is to make a request that sends each of the records to a URL, in this case it is an API URL to edit several products, waiting for parameters (which are each of the fields that are sent in a record) , I have already armed the function to send a single request, I just need to know how I can send several records at once in a single request.
Summary:
I upload the excel file that contains several records. (done)
I keep these records in an array through a function that collects the contents of PhpExcel (done)
Make a POST request with CURL that runs through each of the records and send them at the same time (I have only managed to send only one record)
This code is for call function send POST
private function peticion_get_post($ruta,$formdata,$tipo){
$apiurl = 'https://url/api/v1/';
$url = $apiurl.$ruta;
$postvars = '';
foreach($formdata as $key=>$value) {
$postvars .= $key . "=" . $value . "&";
}
$ch = curl_init();
if($tipo == 'GET'){
curl_setopt($ch,CURLOPT_URL,$url."?".$postvars);
}
if($tipo == 'POST'){
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POSTFIELDS,$postvars);
}
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch,CURLOPT_CUSTOMREQUEST, $tipo);
//curl_setopt($ch,CURLOPT_POST, $tipo);//0 for a GET request y 1 para POST
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT ,0);
curl_setopt($ch,CURLOPT_TIMEOUT, 20);
$respuesta = curl_exec($ch);
$resultado = json_decode($respuesta);
return $resultado;
}
This code is for call function before and sends content from form
$formdata_post = array(
'id_box' => $id_box,
'number' => $number,
'label' => $label
);
$ruta = 'edit.json';
$resultado_post = $this->peticion_get_post($ruta, $formdata_post, 'POST');
I work with Codeigniter, currently, I send the content by JSON through a form and just send a record, I just have to edit the form and send the content that is in the array that generated the PhpExcel when uploading the file.
Related
I am working on a php scraping server , so i have website list to loop and then return the content of each page to get the data that i want.
The problem that some sites are not fully returned and as i see some data appear after the page is fully loaded
I tried with both these methods but i cant get the full page
First method :
$opts = array('http' =>
array(
'method' => 'GET',
'timeout' => 10
) );
$context = stream_context_create($opts);
$html = file_get_contents('some url',false,$context);
echo $html;
Second method
$html = implode('',file('some url'));
echo $html;
I just want to return the content of the page after 1 or 2 seconds after the page is loaded.
For Exemple with this url i cant get the search results just this
: Résultats
News Photos Vidéos Tags Filtre par date
Précédente Suivante
Things are not as they seem to be.
Actually the url that you want to hit is
https://api.swiftype.com/api/v1/public/engines/search.json because the webpage on load makes a json request that is on this url.
in that url you have to post the following json
$search = array("engine_key"=>"naxCjQ58frTkB_diETvu","page"=>1,"q"=>"kardas","per_page"=>12,"sort_direction"=>"","filters"=>array("page"=>array("category"=>"News")),"facets"=>array("page"=>array("0"=>"tag")));
A quick guide:
On the "page" property type a value, that represents the page number you want to get,
on the "q" property type the term that you want to search,
"per_page" property is the entries that you will get, try some
values, 12 is the default,
the rest you have to find them out yourself.
a code example that works
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_URL,"https://api.swiftype.com/api/v1/public/engines/search.json");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode($search));
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8'));
curl_setopt($ch,CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
and to check the results
print_r(json_decode($data));
this thing is beautiful is like them to give you an API on the plate...
Using my current code I need to make a separate curl request for on each row of the smartsheet to get the attachment id, then send another request with that id to get the download url.
This will occur for each row of the smartsheet and is very inefficient for sheets with lots of rows.
Is there a way to get all the attachment id's / url's from a sheet with one request?
class Sheet_request{
private $urlMain = "https://api.smartsheet.com/2.0/users/me";
private $url_food_sheet = "https://api.smartsheet.com/2.0/sheets/3650210866XXXX/?include=attachments";
private $url_food_main= "https://api.smartsheet.com/2.0/sheets/36502108669XXX";
private function curl($url) {
$ch = curl_init($url);
$request_headers = array();
$request_headers[] = "Authorization: Bearer XXXXXXXXXXXXXXXX";
$request_headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if ($response === false) {
die(curl_error($ch));
}
curl_close($ch);
return $response;
}
function get_attachments() {
$response = $this -> curl($this -> url_food_sheet);
$sheetObj = json_decode($response);
foreach ($sheetObj->rows as $row) {
if (isset($row->attachments)){
$rowAttachmentsURL = $this -> url_food_main . "/rows/" . number_format($row -> id, 0, "", "") . "/attachments";
$getAttachmentsResponse = $this->curl($rowAttachmentsURL);
$attachment = json_decode($getAttachmentsResponse);
$attachmentURL = $this->url_food_main . "/attachments/".number_format($attachment->data[0]->id, 0, "", "");
$attachmentInfo = $this->curl($attachmentURL);
$attachmentInfo = json_decode($attachmentInfo);
file_put_contents("pictures/".$attachmentInfo->name, file_get_contents($attachmentInfo->url));
}
}
}
}
$foo = new Sheet_request();
$foo->get_attachments();
You can use the Get All Attachments operation to retrieve all attachments (Ids) in a single request for the specified sheet. As the documentation shows, the Request URI is: https://api.smartsheet.com/2.0/sheets/{sheetId}/attachments.
Please note that Attachments can exist in Smartsheet at 3 different levels: Sheet, Row, Comment. The Get All Attachments response will contain an all attachments in the sheet (at any/all levels: Sheet, Row, Comment) -- so if you're just interested in Row attachments, you'll want to only process items in the response where the parentType attribute has a value of "ROW".
Once you have access to the attachment Ids in the "Get All Attachments" response, you'll still need to subsequently use the Get Attachment operation for each attachment to retrieve the URLs one at a time. (There's currently no way to get these in bulk.)
At the sheet or row level, I believe you can now add the 'include' query parameter to force inclusion of attachments at .sheet. and .row. level, to ensure .comment. level attachments, need to add discussions to the include values:
https://api.smartsheet.com/2.0/sheets/{sheetId}?include=attachments,discussions
I want to process a script every minute using a cron on my server but I need to pass a variable in the URL or some other way. I have researched this and I've seen solutions using arguments in the cron but I don't think that works with what I'm doing.
Here is what I am trying to do:
script.php (runs every minute)
<?php
$marker = $_GET['marker'];
$accountObj = new etAccounts($consumer);
$request_params = new TransactionHistoryRequest();
$request_params->__set('count', 50); //how many will be shown
if($marker_get != ''){
$request_params->__set('marker', $marker_get); //starting point ex. 14293200140265
}
$json = $accountObj->GetTransactionHistory($account, $request_obj, $request_params );
echo $json; //shows most recent 50 transactions starting from marker value
//process json data here...
//included in json is a marker variable that will be used to return the next 50 json results
//after data is processed reload the page with marker in URL
header('Location: script.php?marker=14293200140265');
?>
I understand that cron is CLI on the server side and that it can't process redirections or header locations but how is this possible. I saw someone mention using CURL, how might this work? Example?
Simple example to send post variables to an url:
$fields = array(
'id' => $id,
'mail' => $mail,
);
$url = "yourdomain.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
Based on the comments, you don't need to redirect or use query variables.
You could use a loop that runs while your marker variable is not empty:
$your_marker_variable = '';
do {
$accountObj = new etAccounts($consumer);
$request_params = new TransactionHistoryRequest();
$request_params->__set('count', 50); //how many will be shown
if($marker_get != '') {
$request_params->__set('marker', $marker_get); //starting point ex. 14293200140265
}
$json = $accountObj->GetTransactionHistory($account, $request_obj, $request_params );
echo $json; //shows most recent 50 transactions starting from marker value
//process json data here...
//included in json is a marker variable that will be used to return the next 50 json results
// set the new value of $your_marker_variable
$your_marker_variable = ...
} while (!empty($your_marker_variable));
Note that the undefined and unused variables in the script you posted, make it hard to see what variable is used for what, so you would need to adapt this a bit.
I'm trying to convert this PHP cURL function to work with my rails app. The piece of code is from an SMS payment gateway that needs to verify the POST paramters. Since I'm a big PHP noob I have no idea how to handle this problem.
$verify_url = 'http://smsgatewayadress';
$fields = '';
$d = array(
'merchant_ID' => $_POST['merchant_ID'],
'local_ID' => $_POST['local_ID'],
'total' => $_POST['total'],
'ipn_verify' => $_POST['ipn_verify'],
'timeout' => 10,
);
foreach ($d as $k => $v)
{
$fields .= $k . "=" . urlencode($v) . "&";
}
$fields = substr($fields, 0, strlen($fields)-1);
$ch = curl_init($verify_url); //this initiates a HTTP connection to $verify_url, the connection headers will be stored in $ch
curl_setopt($ch, CURLOPT_POST, 1); //sets the delivery method as POST
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); //The data that is being sent via POST. From what I can see the cURL lib sends them as a string that is built in the foreach loop above
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); //This verifies if the target url sends a redirect header and if it does cURL follows that link
curl_setopt($ch, CURLOPT_HEADER, 0); //This ignores the headers from the answer
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //This specifies that the curl_exec function below must return the result to the accesed URL
$result = curl_exec($ch); //It ransfers the data via POST to the URL, it gets read and returns the result
if ($result == true)
{
//confirmed
$can_download = true;
}
else
{
//failed
$can_download = false;
}
}
if (strpos($_SERVER['REQUEST_URI'], 'ipn.php'))
echo $can_download ? '1' : '0'; //we tell the sms sever that we processed the request
I've googled a cURL lib counterpart in Rails and found a ton of options but none that I could understand and use in the same way this script does.
If anyone could give me a hand with converting this script from php to ruby it would be greatly appreciated.
The most direct approach might be to use the Ruby curb library, which is the most straightforward wrapper for cURL. A lot of the options in Curl::Easy map directly to what you have here. A basis might be:
url = "http://smsgatewayadress/"
Curl::Easy.http_post(url,
Curl::PostField.content('merchant_ID', params[:merchant_ID]),
# ...
)
I have a submit form with method POST, I want to write a script that can automatically submit this form, the reason why I need this is for testing purposes. I need a lot of data in little time in order to test a search based on those form fields, and I do not have time to mannulally do this. Is this possible?
You can use curl to simulate form submit.
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/script.php");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, true ); //enable POST method
// prepare POST data
$post_data = array('name1' => 'value1', 'name2' => 'value2', 'name3' => 'value3');
// pass the POST data
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data );
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
Source:http://php.net/manual/en/book.curl.php
if your not comfortable using curl you could use a php library called snoopy that simulates a web browser. It automates the task of retrieving web page content and posting forms.
<?php
/* load the snoopy class and initialize the object */
require('../includes/Snoopy.class.php');
$snoopy = new Snoopy();
/* set some values */
$p_data['color'] = 'Red';
$p_data['fruit'] = 'apple';
$snoopy->cookies['vegetable'] = 'carrot';
$snoopy->cookies['something'] = 'value';
/* submit the data and get the result */
$snoopy->submit('http://phpstarter.net/samples/118/data_dump.php', $p_data);
/* output the results */
echo '<pre>' . htmlspecialchars($snoopy->results) . '</pre>';
?>
Let PHP fill the form with data and print out a Javascript that posts the form, PHP can not post it on it's own thou.
You can use php.net/curl to send POST requests with PHP.