Unable to pass an ARRAY through CURL? - php

$items[] = array("sku"=>"data","name"=>"data","amount"=>0,"qty"=>"0","id"=>"data","price"=>0,"url"=>"data");
$post = array(
'data' => 'data',
'items' => $items);
$ch = curl_init('urltopost');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
//curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 3); //timeout in seconds
header('Content-Type: text/html');
echo curl_exec($ch);
// Check if any error occurred
if(curl_errno($ch))
{
header('Location: error');
die();
}
If I post it to itself and do var_dump($_POST["items"]); I just get string(5) "Array" as the output.
I also attempted a foreach loop which outputted no data.
Am I being stupid and something glaringly wrong is up with it?

You can try this
$post = [];
foreach($items as $index=>$item){
$new_key = "item[$index]";
foreach($item as $k=>$v){
$post[$new_key.'['. $k .']'] = $v;
}
}
$post['data'] = 'data';
The post array format is array('key'=>string, ....), it not allow array. If you want post array, the key format is 'key[index1][index2] ...'. In server you will receive $_POST['key'] as array.

Related

how to view final url before submission curl php

I want to know final url just before executing curl to check all parameters passing as desired. how to view that.
<?PHP
function openurl($url) {
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch,CURLOPT_POSTFIELDS,$postvars);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_TIMEOUT, '3');
$content = trim(curl_exec($ch));
curl_close($ch);
echo $content;
}
$postvars = array('user' => "user123",'password' => "user#user!123",'Text' => "Test");
$sms_url ="http://remoteserver/plain";
openurl($sms_url);
?>
desired output to check all params and its values passing correct..
http://remoteserver/plain?user=user123&password=user#user!123&Text=TESThere
You forgot to add the $postvars as parameter to your function.
function openurl($url, $postvars) {
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch,CURLOPT_POSTFIELDS,$postvars);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_TIMEOUT, '3');
$content = trim(curl_exec($ch));
curl_close($ch);
echo $content;
}
$postvars = array('user' => "user123",'password' => "user#user!123",'Text' => "Test");
$sms_url ="http://remoteserver/plain";
// create a test var which we can display on screen / log
$test_url = sms_url . http_build_query($postvars);
// either send it to the browser
echo $test_url;
// or send it to your log (make sure loggin is enabled!)
error_log("CURL URL: $test_url", 0);
openurl($sms_url, $postvars);

parse json data from curl

server.php
// some code here .....
// this is the end of the file:
$json_data = array(
'test1'=>hello world,
'test2'=>hello stack
);
echo json_encode($json_data);
api.php
$text = api("http://example.com/?TEXT=$text&APIKEY=$apikey");
// return the json from server.php file
echo $text;
function api($url) {
$ch = curl_init();
$timeout = 20;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
the file api.php return the json successfully
{"test1":"hello world","test2":"hello stack"}
The problem is when I try to parse the returned JSON in order to get the value of test1 it doesn't show anything.
I tried something like this it but can't parse the JSON.
$obj=json_decode($text);
// dosent show anything
echo $obj->test1;
Put quotes around the values in your array.
$json_data = array(
'test1'=>"hello world",
'test2'=>"hello stack");

php curl POST not returnning anything

I have the following php Curl code, to submit a form and get the tables of result
<?php
function httpPost($url,$params)
{
//echo 1;
$postData = '';
//create name value pairs seperated by &
foreach($params as $k => $v)
{
$postData .= $k . '='.$v.'&';
}
rtrim($postData, '&');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, count($postData));
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$output=curl_exec($ch);
curl_close($ch);
//return $params;
return $output;
}
$params = array(
"form_hf_0"=>null,
"searchMode:edit"=>"Births",
"searchSwitch:birthContainer:regNumber:regNumber"=>null,
"searchSwitch:birthContainer:regNumber:regYear"=>null,
"searchSwitch:birthContainer:subjectName:familyName:edit"=>"smith",
"searchSwitch:birthContainer:subjectName:givenName:edit"=>null,
"searchSwitch:birthContainer:subjectName:otherNames:edit"=>null,
"searchSwitch:birthContainer:fatherGivenName:edit"=>null,
"searchSwitch:birthContainer:fatherOtherNames:edit"=>null,
"searchSwitch:birthContainer:motherGivenName:edit"=>null,
"searchSwitch:birthContainer:motherOtherNames:edit"=>null,
"searchSwitch:birthContainer:dateOfEvent:range:edit"=>true,
"searchSwitch:birthContainer:dateOfEvent:switchGroup:range:dateFrom:day"=>01,
"searchSwitch:birthContainer:dateOfEvent:switchGroup:range:dateFrom:month"=>01,
"searchSwitch:birthContainer:dateOfEvent:switchGroup:range:dateFrom:year"=>1788,
"searchSwitch:birthContainer:dateOfEvent:switchGroup:range:dateTo:day"=>31,
"searchSwitch:birthContainer:dateOfEvent:switchGroup:range:dateTo:month"=>12,
"searchSwitch:birthContainer:dateOfEvent:switchGroup:range:dateTo:year"=>1913,
"searchSwitch:birthContainer:district:edit"=>null,
"search-button"=>"Search"
);
$param1 = array("username"=>"sa","password"=>"1");
echo httpPost("https://lifelink.bdm.nsw.gov.au/lifelink/familyhistory/search?0-2.IFormSubmitListener-mainContent-form",$params);
?>
The form link is here:https://lifelink.bdm.nsw.gov.au/lifelink/familyhistory/search?0
I have nothing printed.
Can anyone pointed where is wrong?
The result is here http://ec2-54-213-181-25.us-west-2.compute.amazonaws.com/htdocs/lib/CURL/curl.php
Nothing in the table as normal search with family name "smith", range date 1788 to 1914.

PHP set random variable from proxy list and Curl, if fail repeat

I am trying to figure out how to randomly select a proxy ip from a list and then performing a curl with it, and if a fail occurs use a new proxy ip. Here is my working code without the randomization:
$url = "www.example.com";
$loginpassw = 'myproxypw';
$proxy_ip = '23.27.37.128';
$proxy_port = '29842';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PROXYPORT, $proxy_port);
curl_setopt($ch, CURLOPT_PROXY, $proxy_ip);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $loginpassw);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 600);
$html = curl_exec($ch);
if (strpos($html,'To continue, please type the characters below') !== false) {
echo "now an error has occurred, let's try a new proxy";
}
curl_close($ch);
Ideally the proxy_ip and proxy_port must stay the same in a list of say:
$proxylist = array (
array("ip" => "23.27.37.128", "port" => "29842"),
array("ip" => "23.27.37.111", "port" => "29852"),
array("ip" => "23.27.37.112", "port" => "29742"),
array("ip" => "23.27.37.151", "port" => "29242")
);
I was wondering if I could possibly use shuffle:
shuffle($proxylist);
while($element = array_pop($proxylist)){
return $element;
}
My second question would be the best way of doing this, my PHP is not perfect so I am wondering rather than rewriting the top curl over and over should I store it inside a function?
Any help appreciated.
Thanks,
Simon
Edit:
The following code seems to be working where I have split my code into two functions:
function curltime($url, $proxy_ip, $proxy_port, $loginpassw){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PROXY, $proxy_ip);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $loginpassw);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 600);
return curl_exec($ch);
curl_close($ch);
}
//now let's do the curl
$url = "www.example.com";
$proxylist = array (
array("proxyip" => "23.27.37.128", "proxyport" => "29842"),
array("proxyip" => "23.27.37.111", "proxyport" => "29852"),
array("proxyip" => "23.27.37.112", "proxyport" => "29742"),
array("proxyip" => "23.27.37.151", "proxyport" => "29242")
);
foreach ($proxylist[mt_rand(0,count($proxylist)-1)] as $key => $value) {
$$key = $value;
}
$html = $this->curltime($url, $proxyip, $proxyport, 'somepassword');
if (strpos($html,'To continue, please type the characters below') !== false) {
echo "now we have errors so let's try again"
foreach ($proxylist[mt_rand(0,count($proxylist)-1)] as $key => $value) {
$$key = $value;
}
$html = $this->curltime($url, $proxyip, $proxyport, 'somepassword');
}
$cache .= $html;
Anyone know of a better way for me to do the looping?
To get a random proxy from the list you could use this:
$proxylist[mt_rand(0,count($proxylist)-1)]
Explained:
count($array) Get length of array
mt_rand($x,$y) Get a random number between $x and $y
Edit:
It is totaly possible to do like you did also. Then just always take like the first element of the array.
shuffle($array);
$array[0]
Which of these two options are best for the randomness I can't really say though.

How to read CURL POST on remote server?

This is my cURL POST function:
public function curlPost($url, $data)
{
$fields = '';
foreach($data as $key => $value) {
$fields .= $key . '=' . $value . '&';
}
rtrim($fields, '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($data));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
}
$this->curlPost('remoteServer', array(data));
How do I read the POST on the remote server?
The remote server is using PHP... but what var in $_POST[] should I read
for e.g:- $_POST['fields'] or $_POST['result']
You code works but i'll advice you to add 2 other things
A. CURLOPT_FOLLOWLOCATION because of HTTP 302
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
B. return in case you need to output the result
return $result ;
Example
function curlPost($url, $data) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
return $result;
}
print(curlPost("http://yahoo.com", array()));
Another Example
print(curlPost("http://your_SITE", array("greeting"=>"Hello World")));
To read your post you can use
print($_REQUEST['greeting']);
or
print($_POST['greeting']);
as a normal POST request ... all data posted can be found in $_POST ... except files of course :) add an &action=request1 for example to URL
if ($_GET['action'] == 'request1') {
print_r ($_POST);
}
EDIT: To see the POST vars use the folowing in your POST handler file
if ($_GET['action'] == 'request1') {
ob_start();
print_r($_POST);
$contents = ob_get_contents();
ob_end_clean();
error_log($contents, 3, 'log.txt' );
}

Categories