cURL failing to submit POST data - php

I'm trying to submit a simple form with cURL. After obtaining the login cookie and submitting the data I want, I get a random response and form fails to submit. Here is what it looks like when browser submits a form:
formPost:cur_product_form_new_86286
curCheckbox:Y
C2cProductsListing[business_hr_from]:10:00:00
C2cProductsListing[business_hr_to]:09:30:00
C2cProductsListing[online_hr]:35
C2cProductsListing[offline_hr]:16
C2cProductsListing[non_business_hr]:17
C2cProductsListing[actual_quantity]:25000
C2cProductsListing[minimum_quantity]:25000
C2cProductsListing[products_base_currency]:USD
C2cProductsListing[products_price]:88
delivery[1]:1
C2cProductsListing[c2c_products_listing_id]:86286
Here is what it looks like when I submit the form:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.g2g.com/sell/manageListingInfo'); // open a protected page
curl_setopt($ch, CURLOPT_REFERER, 'http://www.g2g.com/sell/manageListing?game=2522&product_type=19248');
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.A.B.C Safari/525.13");
$data = array(
'formPost' => 'cur_product_form_new_86286',
'curCheckbox' => 'Y',
'C2cProductsListing' => array(
'business_hr_from' => '09:00:00',
'business_hr_to' => '08:30:00',
'online_hr' => '35',
'offline_hr' => '16',
'non_business_hr' => '17',
'actual_quantity' => '25000',
'minimum_quantity' => '25000',
'products_base_currency' => 'USD',
'products_price' => '25',
'c2c_products_listing_id' => '86286'
),
'delivery' => array(
'1' => '1'
)
);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_REFERER, 'http://www.g2g.com/sell/manageListing?game=2522&product_type=19248');
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
print_r(curl_getinfo($ch));
curl_close($ch);
And here is the completely random response I get:
Array ( [url] => http://www.g2g.com/sell/manageListingInfo [content_type] => [http_code] => 0 [header_size] => 0 [request_size] => 0 [filetime] => 0 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0 [namelookup_time] => 0 [connect_time] => 0 [pretransfer_time] => 0 [size_upload] => 0 [size_download] => 0 [speed_download] => 0 [speed_upload] => 0 [download_content_length] => -1 [upload_content_length] => -1 [starttransfer_time] => 0 [redirect_time] => 0 [certinfo] => Array ( ) )
The array I just posted is result of print_r(curl_getinfo($ch)). How do I end up here? I emulated the browser request fully (aside from it being cURL and not the actual browser) using all the data necessary, yet the form doesn't seem to be submitted?

You're not calling curl_exec to actually execute the query. Call curl_getinfo afterwards. If you want the data returned use the return value of curl_exec instead of curl_getinfo. For example:
$data = curl_exec($ch);
echo $data;

Related

Final URL - PHP (Effective URL) How to

I apologize in advance for my English.
I need from a url http://www.streamuj.tv/video/2f15014c90a9f62af511?streamuj=hd&authorize=7736cdf0f3719ed75b26132aee184525 get the final redirected to url redirection (.flv) url.
I tried this, but somehow it doesn't get the info I need:
<?php
function getMainUrl($url) {
$headers = get_headers($url, 1);
return $headers['Location'];
}
echo getMainUrl("http://www.streamuj.tv/video/2f15014c90a9f62af511?streamuj=hd&authorize=7736cdf0f3719ed75b26132aee184525");
?>
When use:
http://getlinkinfo.com/info?link=http%3A%2F%2Fwww.streamuj.tv%2Fvideo%2F2f15014c90a9f62af511%3Fstreamuj%3Dhd%26authorize%3D7736cdf0f3719ed75b26132aee184525+&x=45&y=6
it redirects to:
http://s14.streamuj.tv:8080/vid/8f18cade6df7fc2d54a3522e7515771e/58a1ecfa/2f15014c90a9f62af511_hd.flv?start=0
and this is the one I need from php
Any help would be appreciated. Thanks
Using cURL, we tell it only to fetch the header and to follow redirects.
<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://www.streamuj.tv/video/2f15014c90a9f62af511?streamuj=hd&authorize=7736cdf0f3719ed75b26132aee184525');
curl_setopt($curl, CURLOPT_FILETIME, true);
curl_setopt($curl, CURLOPT_NOBODY, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
$header = curl_exec($curl);
$info = curl_getinfo($curl);
curl_close($curl);
print_r($info);
Outputs:
Array
(
[url] => http://s14.streamuj.tv:8080/vid/c7194f1279591f88c162382a0a5a49d1/58a1f0a5/2f15014c90a9f62af511_hd.flv?start=0
[content_type] => video/x-flv
...
)
This Work:
<?php
session_start();
include "simple_html_dom.php";
$proxy = array("88.159.43.160:80");
$proxyNum = 0;
$proxy = explode(':', $proxy[$proxyNum]);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://www.streamuj.tv/video/2f15014c90a9f62af511?streamuj=hd&authorize=7736cdf0f3719ed75b26132aee184525');
curl_setopt($curl, CURLOPT_FILETIME, true);
curl_setopt($curl, CURLOPT_NOBODY, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($curl, CURLOPT_PROXY, $proxy[0]);
curl_setopt($curl, CURLOPT_PROXYPORT, $proxy[1]);
$header = curl_exec($curl);
$info = curl_getinfo($curl);
curl_close($curl);
print_r($info);
?>
Outputs:
Array ( [url] => http://s14.streamuj.tv:8080/vid/0b9ac442e33b2f0fe72dd45295b6e7bd/58a1f3b0/2f15014c90a9f62af511_hd.flv?start=0 [content_type] => video/x-flv [http_code] => 200 [header_size] => 738 [request_size] => 326 [filetime] => 1481886073 [ssl_verify_result] => 0 [redirect_count] => 1 [total_time] => 3.485 [namelookup_time] => 0 [connect_time] => 3.063 [pretransfer_time] => 3.063 [size_upload] => 0 [size_download] => 0 [speed_download] => 0 [speed_upload] => 0 [download_content_length] => 674855667 [upload_content_length] => -1 [starttransfer_time] => 3.188 [redirect_time] => 0.297 [redirect_url] => [primary_ip] => 88.159.43.160 [certinfo] => Array ( ) [primary_port] => 80 [local_ip] => 192.168.0.8 [local_port] => 57532 )
How to can please echo only this: http://s14.streamuj.tv:8080/vid/0b9ac442e33b2f0fe72dd45295b6e7bd/58a1f3b0/2f15014c90a9f62af511_hd.flv?start=0
?

php curl fails because of url

I wrote a script that is sending some data to an external server. Everything is working fine so far. The problem is when I'm building the url by a variable, which is necessary, the post request won't succeed. When I'm echoing out both variables they're exactly the same. The $action variable is coming through an array and has the value of /index.php?param=value.
$regexp = '/<form(.*?)action="(.*?)"(.*?)>(.*?)<\/form>/';
preg_match_all($regexp, $body, $form);
$action = $form[2][0];
$url = 'https://www.homepage.com/index.php?param=value'; // success
$url = 'https://www.homepage.com'.$action; // no success
$post_data = array(
'param1' => 'value1',
'param2' => 'value2'
);
$data = array(
'url' => $url,
'post_data' => $post_data
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3 AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false );
curl_setopt($ch, CURLOPT_ENCODING, "" );
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_COOKIEFILE, getcwd () . '/cookie.txt' );
curl_setopt($ch, CURLOPT_COOKIEJAR, getcwd () . '/cookie.txt' );
curl_setopt($ch, CURLOPT_URL, $url);
curl_exec($ch);
There is no curl_error($ch) and for curl_getinfo($ch) I get the following in both cases:
Array
(
[url] => https://www.homepage.com/index.php?param=value
[content_type] => text/html; charset=utf-8
[http_code] => 200
[header_size] => 437
[request_size] => 865
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.12154
[namelookup_time] => 2.1E-5
[connect_time] => 0.003557
[pretransfer_time] => 0.014587
[size_upload] => 468
[size_download] => 1248
[speed_download] => 10268
[speed_upload] => 3850
[download_content_length] => -1
[upload_content_length] => 468
[starttransfer_time] => 0.057372
[redirect_time] => 0
[redirect_url] =>
[primary_ip] => 00.00.00.00 // changed by myself
[certinfo] => Array
(
)
[primary_port] => 443
[local_ip] => 000.000.000.000 // changed by myself
[local_port] => 62352
)
EDIT
When I am doing
$url_1 = 'https://www.homepage.com/index.php?param=value'; // success
$url_2 = 'https://www.homepage.com'.$action; // no success
if ($url_1 == $url_2) {
echo 'MATCH';
} else {
echo $url_1.'_';
echo $url_2.'_';
}
I get the else statement. So they are not the same. But they are looking identically. _ in else statement is for looking after white spaces.

curl error HTTP status code 0-- empty reply from server

I'm trying to scrap a website but it always said that Empty Reply from server
can any one look at the code and tell me what am I doing wrong?
Here is the code
function spider($url){
$header = array(
"Host" => "www.example.net",
//"Accept-Encoding:gzip,deflate,sdch",
"Accept-Language:en-US,en;q=0.8",
"Cache-Control:max-age=0",
"Connection:keep-alive","Content-Length:725","Content-Type:application/x-www-form-urlencoded",
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'
,"X-Requested-With:XMLHttpRequest"
);
$cookie = "cookie.txt";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0); // return headers 0 no 1 yes
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return page 1:yes
curl_setopt($ch, CURLOPT_TIMEOUT, 200); // http request time-out 20 seconds
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Follow redirects, need this if the URL changes
curl_setopt($ch, CURLOPT_MAXREDIRS, 2); //if http server gives redirection response
curl_setopt($ch, CURLOPT_USERAGENT,
"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7");
curl_setopt($ch, CURLOPT_COOKIEJAR, realpath( $cookie)); // cookies storage / here the changes have been made
curl_setopt($ch, CURLOPT_COOKIEFILE, realpath( $cookie));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // false for https
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,"view=ViewDistrict&param=7&uniqueid=1397991494188&PHPSESSID=f134vrnv7glosgojvf4n1mp7o2&page=http%3A%2F%2Fwww.example.com%2Fxhr.php");
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_REFERER, "http://www.example.com/");
$data = curl_exec($ch); // execute the http request
$info = curl_getinfo($ch);
curl_close($ch); // close the connection
return $data;
}
Here is function call
echo spider("http://www.example.net/");
Edit
Array ( [url] => http://www.example.net/ [content_type] => text/html [http_code] => 301 [header_size] => 196 [request_size] => 840 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 1 [total_time] => 61.359 [namelookup_time] => 0 [connect_time] => 0.281 [pretransfer_time] => 0.281 [size_upload] => 0 [size_download] => 0 [speed_download] => 0 [speed_upload] => 0 [download_content_length] => 178 [upload_content_length] => 0 [starttransfer_time] => 60.593 [redirect_time] => 0.766 [certinfo] => Array ( ) [redirect_url] => ) Empty reply from server
this is the header now
also I'd updated my post data
it's now
curl_setopt($ch, CURLOPT_POSTFIELDS,"view=ViewDistrict&param=7&uniqueid=".time(). rand(101,500)."&PHPSESSID=f134vrnv7glosgojvf4n1mp7o2&page=http%3A%2F%2Fexample.com%2Fxhr.php");
and also had removed "X-Requested-With:XMLHttpRequest" from headers
Have you tried removing this from the headers ?
X-Requested-With:XMLHttpRequest
My guess is that your problem is in this line:
curl_setopt(
$ch,
CURLOPT_POSTFIELDS,
"view=ViewDistrict&param=7&uniqueid=1397991494188&PHPSESSID=f134vrnv7glosgojvf4n1mp7o2&page=http%3A%2F%2Fwww.example.com%2Fxhr.php"
);
Notice that you're passing a value for PHPSESSID. I'm guessing you copied & pasted a URL from a visit to the site, right? That session ID was probably valid when you visited the site, but the odds of it being valid now are pretty slim. And if the server doesn't like the session ID, chances are it's not going to give you any data.

Cannot post data with PHP curl

I'm trying to POST some data to a web service with cURL, using the following code:
$response = "<p>Here is your RMA information. Please ship the product to the address below.<br>
<br>
IMPORTANT:<br>
1) Refunds can only be issues for purchases made within 30 days. Products missing any accessories or original packaging will require a 20% restocking fee.<br>Please see our RMA Guidelines here:<br>
We will update you with the progress of the RMA once we receive your RMA. We appreciate your patience and understanding.<br>
</p>";
$url = "https://mysite.desk.com/api/v2/cases/18/notes";
$username = "my username";
$password = "my password";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"body":"' . $response . '"}');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$output = curl_exec($ch);
it gives me following error
[url] => https://mysite.desk.com/api/v2/cases/18/notes
[content_type] =>
[http_code] => 500
[header_size] => 165
[request_size] => 228
[filetime] => -1
[ssl_verify_result] => 20
[redirect_count] => 0
[total_time] => 1.373
[namelookup_time] => 0
[connect_time] => 0.281
[pretransfer_time] => 0.811
[size_upload] => 2235
[size_download] => 0
[speed_download] => 0
[speed_upload] => 1627
[download_content_length] => -1
[upload_content_length] => 2235
[starttransfer_time] => 1.092
[redirect_time] => 0
I don't understand exact problem. please help me.
try array if json format data seem to be complicated
$post_array=array('body'=>$response);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_array;
It is solved by changing following lines
$data = array();
$data['body'] = preg_replace("/&#?[a-z0-9]{2,8};/i", "", strip_tags($_POST['response']));
and
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

Trying to get connects of URL with PHP CURL , but no success

here is my URL: https://webservices-dev.compuscan.co.za:9443/PersonStatusService/user2/password2/8310240031083/XML
i am try to get its content with PHP curl but no success hope some can help.
here is my code:
// you can add anoother curl options too
// see here - http://php.net/manual/en/function.curl-setopt.php
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
//curl_setopt($ch, CURLOPT_POST, TRUE); // Use POST method
//curl_setopt($ch, CURLOPT_POSTFIELDS, "var1=1&var2=2&var3=3"); // Define POST values
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
$output = curl_getinfo($ch);
print_r($output);
curl_close($ch);
return $data;
}
$url = "https://webservices-dev.compuscan.co.za:9443/PersonStatusService/user2/password2/8310240031083/XML" ;
$variablee = get_data($url);
echo $variable;
Well i am getting results as expected.
It should be
$variablee = get_data($url);
echo $variablee; //<--- Must be $variablee (as you have defined it above)
OUTPUT:
Array ( [url] =>
https://webservices-dev.compuscan.co.za:9443/PersonStatusService/user2/password2/8310240031083/XML
[content_type] => text/plain; charset=utf-8 [http_code] => 200
[header_size] => 81 [request_size] => 192 [filetime] => -1
[ssl_verify_result] => 19 [redirect_count] => 0 [total_time] => 1.544
[namelookup_time] => 0 [connect_time] => 0.343 [pretransfer_time] =>
1.186 [size_upload] => 0 [size_download] => 656 [speed_download] => 424 [speed_upload] => 0 [download_content_length] => 656
[upload_content_length] => 0 [starttransfer_time] => 1.544
[redirect_time] => 0 [certinfo] => Array ( ) [primary_ip] =>
196.34.30.23 [primary_port] => 9443 [local_ip] => 192.168.72.37 [local_port] => 61090 [redirect_url] => )
8310240031083CLAIRECSAPLARN1983/10/24FY2012/03/242013/09/308 SKALIE
STWEST ACRES EXT 1312012013/08/130768333118
function get_data($url) {
$ch = curl_init($url);
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0;
Windows NT 6.0)");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpCode == 404) {
curl_close($ch);
return '404';
} else {
curl_close($ch);
return $data;
}
}
This function worked perfectly for me, maybe somebody finds it useful as I did. I'm not pretending to answer your question (well, I'm 9 months late) but solve people's similar questions about a similar issue.

Categories