PHP Curl Multi post json data - php

I got stuck for 2 hours on this php curl multiple request
i wantta make post json data start from 1111(This is a start point and as a verificationCode) to 1121(end point 1111 + $process_count)
check this out guys :
<?php
$url = "https://api.mywebsite.com/myapp/customer/verification";
$mh = curl_multi_init();
$handles = array();
$process_count = 10;
for($c=1111;$c <= 1121;$c++){
$data_verification = array(
"phone" => "+6285643103039", // +6285643103039 9025
"verificationCode" => $c
);
$str_verification = json_encode($data_verification);
}
while ($process_count--)
{
$ch = curl_init($url);
$headers= array('Accept: application/json','Content-Type: application/json');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_TIMEOUT, 4000);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS,$str_verification);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
ob_start();
curl_multi_add_handle($mh, $ch);
$handles[] = $ch;
}
$running=null;
do
{
curl_multi_exec($mh, $running);
}
while ($running > 0);
for($i = 0; $i < count($handles); $i++)
{
$out = curl_multi_getcontent($handles[$i]);
echo "$i. ";
print $out . "\r\n";
echo "<br>";
curl_multi_remove_handle($mh, $handles[$i]);
}
curl_multi_close($mh);
?>
But
curl_setopt($ch, CURLOPT_POSTFIELDS,$str_verification); always given end point value 1121.
And doesn't looping
from 1111 to 1121.
Anyone can figure it out ? i'll glad for any help

You are doing a mistake in your first loop, you are erasing data every time into only one variable and not array
$str_verification = json_encode($data_verification);
Here is what I suggest you to do :
$str_verification = array();
for($c=1111;$c <= 1121;$c++){
$data_verification = array(
"phone" => "+6285643103039", // +6285643103039 9025
"verificationCode" => $c
);
$str_verification[] = json_encode($data_verification);
}
for ($i = 0; $i != 10; $i++)
{
$ch = curl_init($url);
$headers= array('Accept: application/json','Content-Type: application/json');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_TIMEOUT, 4000);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS,$str_verification[$i]);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
ob_start();
curl_multi_add_handle($mh, $ch);
$handles[] = $ch;
}

Related

Check proxy before cURL

I need to check whether the selected $proxy is active or not and do a loop until a working one in proxy.txt is found before using it in a cURL. How could I do that? Below is a given proxy function.
<?php
function proxies()
{
$proxylist = file("proxy.txt");
$randomproxy = rand(0, sizeof($proxylist) - 1);
$proxylist = $proxylist[$randomproxy];
$words = explode(':', $proxylist);
return $words;
}
$proxy = proxies();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'url');
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5_HOSTNAME);
curl_setopt($ch, CURLOPT_PROXY, $proxy[0]);
curl_setopt($ch, CURLOPT_PROXYPORT, $proxy[1]);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, "$username:$password");
....
?>
Thank you.
maybe you can ping, something like this
function proxies()
{
return file("proxy.txt");
}
foreach ($proxies() as $proxy) {
$item = explode(':', $proxy);
$waitTimeoutInSeconds = 1;
if ($fp = fsockopen($item[0], $item[1], $errCode, $errStr, $waitTimeoutInSeconds)){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'url');
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5_HOSTNAME);
curl_setopt($ch, CURLOPT_PROXY, $proxy[0]);
curl_setopt($ch, CURLOPT_PROXYPORT, $proxy[1]);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, "$username:$password");
break;
}
}
fclose($fp);

Curl (can't hide output)

i maked this function:
function makeRequest($url,$browser,$ip,$cookie = false,$referrer = null)
{
if($referrer==null)
{
$refferer = 'http://google.com';
}
$headers = array();
$headers[] = "HTTP_X_FORWARDED_FOR: ".$ip;
$headers[] = "X_FORWARDED_FOR: ".$ip;
$headers[] = "REMOTE_ADDR:". $ip;
$headers[] = "REFERRER: ".$referrer;
$headers[] = $browser;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_COOKIESESSION, true );
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
if($cookie!==false)
{
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_HEADER, false);
}
else
{
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
}
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_REFERER, $referrer);
if($cookie==false)
{
$return = curl_exec($ch);
}
else
{
$return = 'hidden content';
curl_exec($ch);
}
curl_close($ch);
return $x;
}
But everytime returning the content, if i already set cookie to false.
I don't know how can i do that.
I tried with:
CURLOPT_RETURNTRANSFER,
CURLOPT_VERBOSE,
CURLOPT_HEADER
But nothing...
Thank's (sorry for my eng)

Rebuilding the script from cURL to multi cURL

I'm trying to implement some multi cURL functions instead of simple cURL functions.
I have the following snippet:
$curl = curl_init();
curl_setopt($curl, CURLOPT_ENCODING,'gzip');
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, $url);
$result = curl_exec($curl);
curl_close($curl);
$rv = ($returnArray) ? json_decode($result, true) : json_decode($result);
It gives me results ($result returns success and some data). I want to rewrite it to use curl_multi_init(). I tried this:
$curl = curl_init();
curl_setopt($curl, CURLOPT_ENCODING,'gzip');
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, $url);
$mh = curl_multi_init();
curl_multi_add_handle($mh,$curl);
$running= \null;
do {
curl_multi_exec($mh,$running);
$result = curl_multi_exec($mh,$running);
} while($running > 0);
curl_multi_remove_handle($mh, $curl);
curl_multi_close($mh);
$rv = ($returnArray) ? json_decode($result, true) : json_decode($result);
I get no results ($result is empty). I have no errors whatsoever. What is wrong?
This Works for me :
function get_result( $nodes )
{
$node_count = count($nodes);
$curl_arr = array();
$master = curl_multi_init();
for($i = 0; $i < $node_count; $i++)
{
$url = $nodes[$i];
$curl_arr[$i] = curl_init($url);
curl_setopt($curl_arr[$i], CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_arr[$i], CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($curl_arr[$i], CURLOPT_TIMEOUT, 10);
curl_setopt($curl_arr[$i], CURLOPT_ENCODING, "gzip");
curl_setopt($curl_arr[$i], CURLOPT_VERBOSE, true);
curl_setopt($curl_arr[$i], CURLOPT_USERAGENT, 'Mozilla/5.0');
curl_setopt($curl_arr[$i], CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl_arr[$i], CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_multi_add_handle($master, $curl_arr[$i]);
}
do {
curl_multi_exec($master,$running);
curl_multi_select($master, 5.0);
} while($running > 0);
$output = "";
for($i = 0; $i < $node_count; $i++)
{
$output .= curl_multi_getcontent( $curl_arr[$i] ) . "<breaktag>";
curl_multi_remove_handle( $master, $curl_arr[$i] );
curl_close( $curl_arr[$i] );
}
curl_multi_close( $master );
return $output;
}
$nodes[] = () // your URLs
$responses = get_result( $nodes );
$responses = explode("<breaktag>", $responses); //now responses is array of result

Random 500 errors in a multi CURL request

I am trying to get some hal+json data from a web service through a curl_multi to fill in a Bootstrap Typeahead. Every time I run this code, some of my requests in the curl_multi will be 500, and some will return the data I need. The ones that are 500 are completely random; the next time I load the page different queries will be 500 instead (And I get no errors). Why does that keep happening?
<?php
$curlurl = 'https://service.domain.com/'.$_POST['customer-name'].'/contact?callback=?';
$cuurl = singleRequest($curlurl);
global $contacts_typeahead_data;
$contacts_typeahead_data = array();
$clinks = $cuurl['_links']['https://service.domain.com/rel/contacts'];
for ($i=0; $i < count($clinks); $i++) {
$data[] = 'https://service.domain.com'.$clinks[$i]['href'].'?callback=?';
};
print_r($data);
$datar = multiRequest($data);
print_r($datar);
foreach($datar as $c){
$cs = json_decode($c, true);?>
<option value="<?php echo $cs['id']; ?>"><?php echo $cs['id'].' ('.$cs['info'][0]['name'].')'; ?></option>
<?php $contacts_typeahead_data[] = $cs['id'].' ('.$cs['info'][0]['name'].')';
}?>
And here is the code to the singleRequest and multiRequest, which is based on http://www.phpied.com/simultaneuos-http-requests-in-php-with-curl/:
function multiRequest($data, $options = array()) {
// array of curl handles
$curly = array();
// data to be returned
$result = array();
// multi handle
$mh = curl_multi_init();
// loop through $data and create curl handles
// then add them to the multi-handle
foreach ($data as $id => $d) {
$curly[$id] = curl_init();
$url = (is_array($d) && !empty($d['url'])) ? $d['url'] : $d;
$username = base64_encode('username');
$password2 = 'password';
$auth_token = $username . $password2 . 'QQ==';
curl_setopt($curly[$id], CURLOPT_URL, $url);
curl_setopt($curly[$id], CURLOPT_HEADER, 0);
curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curly[$id], CURLOPT_USERPWD, "$username:$password2");
curl_setopt($curly[$id], CURLOPT_SSLVERSION,3);
curl_setopt($curly[$id], CURLOPT_HTTPHEADER, array(
'Accept: application/hal+json',
'Access-Control-Allow-Origin: *',
'Content-Type: application/hal+json',
'Authorization: Basic ' . $auth_token
));
curl_setopt($curly[$id], CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curly[$id], CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($curly[$id], CURLOPT_TIMEOUT, 30);
curl_setopt($curly[$id], CURLOPT_PORT, 443);
curl_setopt($curly[$id], CURLOPT_REFERER, $_SERVER['HTTP_REFERER']);
curl_setopt($curly[$id], CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($curly[$id], CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curly[$id], CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curly[$id], CURLOPT_COOKIE, 'cert_url=https://service.domain.com/cert/yagowyefayygwflagliwygelifyaigwepifgpai');
// post?
if (is_array($d)) {
if (!empty($d['post'])) {
curl_setopt($curly[$id], CURLOPT_POST, 1);
curl_setopt($curly[$id], CURLOPT_POSTFIELDS, $d['post']);
}
}
// extra options?
if (!empty($options)) {
curl_setopt_array($curly[$id], $options);
}
curl_multi_add_handle($mh, $curly[$id]);
}
// execute the handles
$running = null;
do {
curl_multi_exec($mh, $running);
} while($running > 0);
// get content and remove handles
foreach($curly as $id => $c) {
// cURL error number
$curl_errno = curl_errno($c);
// cURL error message
$curl_error = curl_error($c);
// output if there was an error
if ($curl_error) {
echo " *** cURL error: (".$curl_errno.") ".$curl_error;
} else {
$result[$id] = curl_multi_getcontent($c);
}
curl_multi_remove_handle($mh, $c);
}
// all done
curl_multi_close($mh);
return $result;
}
function singleRequest($curlurl){
$username = base64_encode('username');
$password2 = 'password';
$auth_token = $username . $password2 . 'QQ==';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password2");
curl_setopt($ch, CURLOPT_SSLVERSION,3);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/hal+json',
'Access-Control-Allow-Origin: *',
'Content-Type: application/hal+json',
'Authorization: Basic ' . $auth_token
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_COOKIE, 'cert_url=https://service.domain.com/cert/yagowyefayygwflagliwygelifyaigwepifgpai');
$result = curl_exec($ch);
if($result === false){
echo 'Curl error: ' . curl_error($ch). '<br><br>';
print_r(error_get_last());
}
$response = json_decode($result, true);
curl_close($ch);
return $response;
}
Edit Added error check
Why does that keep happening?
The server you send the request to makes this happen. It is just that requests can fail.
Deal with it and design for failure.

PHP CURL post request and Error 417

function query($url, $pfields = 0, $cookie = 0)
{
curl_setopt($ch, CURLOPT_HEADER, 1);
if (!empty($pfields))
{
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $pfields);
}
if (!empty($cookie))
{
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_ENCODING,'gzip');
if (!$login)
{
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
}
$content = curl_exec($ch);
return $content;
}
$cookie = 'sessionID=3864cab58412ec567b634db3c317898;OAGEO=RU%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C;';
$p = '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++';
$post = 'clientid=23&campaignid=52&bannerid=111&appendsave=1&appendtype=0&append=' . urlencode($p) . '&submitbutton=';
echo query('http://example.com/in.php', $post, $cookie);
This code is returned 417 error(
BUT $p is not usage urlencode but IS OK but +(plus) change for " "(space)
Sooooorry for my very bad english
Try adding this:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));

Categories