Hi is curl the best method for using POST with cookie and also navigated to another page for scrapping? I'm using the coding below and I can't get it to work.
include('simple_html_dom.php');
$data = array(
'__EVENTTARGET' => '',
'__EVENTARGUMENT' => '',
'__VIEWSTATE' => '%2FwEPDwUKLTcyODA2ODEwMGRk',
'Myname' => 'justdev12345',
'Mypassword' => '12345',
'idLogin' => 'Login',
'tmplang2' => '6',
'fm' => '',
'jc' => '',
'LoginRef' => ''
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://secure.site.com/mainframe.aspx");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_COOKIEJAR, "sdc_cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "sdc_cookies.txt");
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
$output = new simple_html_dom();
$output = file_get_html('http://profiles.site.com/profile_900.aspx?AccountID=ShopCartUpdate');
print $output;
Your code is fine up until the end where you throw away the curl response and load the url with simple-html-dom. If you want to use the curl response it should look like this:
$html = str_get_html($output);
$html->find('title');
Otherwise you should probably remove all the curl code.
Related
Essentially the following script is working fine when in local host but fails to load (Timeout) anytime loaded from a web server, is their something incorrectly written? I know it's not the end point api url that's the issue, since it works on local. Please see below, wondering if it's caused by curl_setopt:
$id = "dhl";
$number = "039103913";
// Parameters setting
$key = 'key';
$secret = 'scret';
$param = array (
'id' => $id,
'number' => $number,
'phone' => '',
'ship_from' => '',
'ship_to' => '',
'area_show' => 1,
'order' => 'desc'
);
// Request Json
$json = json_encode($param, JSON_UNESCAPED_UNICODE);
$signature = strtoupper(md5($json.$key.$secret));
$url = 'https://www.kd100.com/api/v1/tracking/realtime';
$headers = array (
'Content-Type:application/json',
'API-Key:'.$key,
'signature:'.$signature,
'Content-Length:'.strlen($json)
);
// Send post request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$result = curl_exec($ch);
$data = json_decode($result, true);
echo json_encode($data);
Good day! I'm trying to load an image using the method appWidgets.saveAppImage. In the beginning I receive URL the server for loading -> getAppImageUploadServer, there all ok!
I receive a hash and an image, send them a POST request and get an error. Here is my code:
$token = "Service_access_key";
$tmp_image = file_get_contents('https://www.ejin.ru/wp-content/uploads/2017/12/667108931864_667108931864-150x150.jpg');
file_put_contents(dirname(__FILE__).'/tmp.jpg',$tmp_image);
$img_path = dirname(__FILE__).'/tmp.jpg';
$post_data = array("image" => "#".$img_path);
$upload_url = file_get_contents("https://api.vk.com/method/appWidgets.getAppImageUploadServer?v=5.85&image_type=50x50&access_token=".$token);
$url = json_decode($upload_url)->response->upload_url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$result = json_decode(curl_exec($ch),true);
$safe = file_get_contents("https://api.vk.com/method/appWidgets.saveAppImage?v=5.85&hash=".$result['hash']."&image=".$result['image']."&access_token=".$token);
echo $safe;
echo:
"error_code":129,"error_msg":"Invalid photo: file not found, from upl_850128?act=app_widget_image"
what's my mistake?
<?
$request_params = array(
'image_type' => '510x128',
'access_token' => 'xxx',
'v' => '5.92'
);
$t = json_decode(file_get_contents('https://api.vk.com/method/appWidgets.getAppImageUploadServer?'. http_build_query($request_params)));
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:multipart/form-data"));
curl_setopt($ch, CURLOPT_URL, $t->response->upload_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, array("image" => new CURLFile(dirname(__FILE__).'\img.jpg')));
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
$result = json_decode(curl_exec($ch));
curl_close($ch);
$request_params = array(
'hash' => $result->hash,
'image' => $result->image,
'access_token' => 'xxx',
'v' => '5.92'
);
print_r(file_get_contents('https://api.vk.com/method/appWidgets.saveAppImage?'. http_build_query($request_params)));
?>
I'm trying to post files to a 3rd party API from my PHP app.
This works from the command line:
curl -F "file=#move_file.MOV" "https://upload.wistia.com?project_id=pbmcmua3ot&username=api&api_password=xxxxx_apikey_yyyyy"
But I can't get it to work using PHP's curl:
$data = array(
'username' => $username,
'api_password' => $api_password,
'file' => fopen($tmp_filename, 'r'),
'project_id' => $project_hashed_id,
);
$ch = curl_init();
//curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, "https://upload.wistia.com" );
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 86400); // 1 Day Timeout
curl_setopt($ch, CURLOPT_INFILE, fopen($tmp_filename, 'r') );
curl_setopt($ch, CURLOPT_NOPROGRESS, false);
curl_setopt($ch, CURLOPT_BUFFERSIZE, 128);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($tmp_filename));
curl_setopt($ch, CURLOPT_VERBOSE, 1); //for debugging
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result);
I think CURLOPT_INFILE is the problem but I'm not sure. Thanks in advance for any help you can give me.
UPDATE1
$data = array(
'username' => $username,
'api_password' => $api_password,
'file' => '#'.$tmp_filename,
'project_id' => $project_hashed_id,
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, "https://upload.wistia.com" );
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 86400); // 1 Day Timeout
curl_setopt($ch, CURLOPT_NOPROGRESS, false);
curl_setopt($ch, CURLOPT_BUFFERSIZE, 128);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($tmp_filename));
curl_setopt($ch, CURLOPT_VERBOSE, 1); //for debugging
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result);
UPDATE 2 (Working)
$data = array(
'api_password' => $api_password,
'file' => '#'.$tmp_filename,
'project_id' => $project_id
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, "https://upload.wistia.com" );
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
curl_close($ch);
You don't need to open a handle for curl, just have
$data = array(
'file' => '#move_file.MOV'
);
Curl will see the # and treat it as a file upload attempt. You also don't have to do http_build_query() either. Curl can accept an array directly and do the query building itself:
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
I'm having trouble using cURL for a specific page.
A live code working: http://svgen.com/jupiter.php
Here is my code:
$url = 'https://uspdigital.usp.br/jupiterweb/autenticar';
$data = array('codpes' => 'someLogin', 'senusu' => 'somePass', 'Submit' => '1');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_COOKIEJAR, "FileHere");
curl_setopt($ch, CURLOPT_COOKIEFILE, "FileHere");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);
curl_exec($ch);
curl_close($ch);
Although I had used the same url and post data, file_get_contents worked:
$options = array('http' => array('method' => 'POST','content' => http_build_query($data)));
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result);
Someone could help me?
Thanks.
Most probably it is the SSL verification problem.
Add
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
Also if you use CURLOPT_PROTOCOLS option, it should be HTTPS since you are posting to a secure url
curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS); // you currently have http
$data = array('codpes' => 'someLogin', 'senusu' => 'somePass', 'Submit' => '1');
should have been
$data = http_build_query(array('codpes' => 'someLogin', 'senusu' => 'somePass', 'Submit' => '1'));
It automatically url encodes your query string as well and is safer than manual methods..
make your post data as:
$data = array('codpes' => 'someLogin', 'senusu' => 'somePass', 'Submit' => '1');
$postData = "";
foreach( $data as $key => $val ) {
$postData .=$key."=".$val."&";
}
$postData = rtrim($postData, "&");
and change:
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
to
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
Try these:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_CAINFO, '/etc/pki/tls/cert.pem'); // path is valid for RHEL/CentOS
This makes sure the resource you're "curling" has a valid SSL certificate. It is not recommended to set "CURLOPT_SSL_VERIFYPEER" to false (0).
You're on a secure connection, why are you using :
curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);
Use instead :
curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
i need to post multiple message to facebook wall from the mysql database. first i fetch the data from mysql and put it in while loop
while($row=mysql_fetch_array($result))
{
$des=$row[1];
$purpose=$row[3];
$price_sale=$row[4];
$price_rent=$row[5];
$img="example.com/images".mysql_result($result,0,2);
$attachment = array(
'access_token' => "$token",
'message' => $des,
'picture' => $img,
'link' => "example.com"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/xxxxxxxxxxx/feed');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //to suppress the curl output
$result = curl_exec($ch);
curl_close ($ch);
echo $result;
}
the $result contain 3 records. But only post the first row. Plz give a solution for this
Try changing the name of the variable that accepts the curl output.You are using the same variable above.
while($row=mysql_fetch_array($result))
{
$des=$row[1];
$purpose=$row[3];
$price_sale=$row[4];
$price_rent=$row[5];
$img="example.com/images".mysql_result($result,0,2);
$attachment = array(
'access_token' => "$token",
'message' => $des,
'picture' => $img,
'link' => "example.com"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/xxxxxxxxxxx/feed');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //to suppress the curl output
$curlresult = curl_exec($ch);
curl_close ($ch);
echo $curlresult;
}