PHP code file: sms1.php
Located at http://techmentry.com/sms1.php (please visit the link and see the outpur of the below code).
<?php
//Variables to POST
$user = "HIDDEN";
$password = "HIDDEN";
$mobiles = "919999999999";
$message = "test";
$sender = "HIDDEN";
//Initialize CURL data to send via POST
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://example.com/example.php");
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "user=$user&
password=$password&
mobiles=$mobiles&
message=$message&
sender=$sender"
);
//Execute CURL command and return into variable $result
$result = curl_exec($ch);
//Do stuff
echo "$result"
?>
The output I expected is: The above code should have process this URL: URL HIDDEN
and then return a message ID or appropiate error code.
But the output I got is beyond my expection! There are even no error logs. Please help me :)
You get the code, but you also get the complete response from the server, including HTTP headers.
I get:
HTTP/1.1 200 OK
Date: Wed, 24 Jul 2013 12:24:01 GMT
Server: Apache
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: max-age=60, private, proxy-revalidate
Pragma: no-cache
Vary: Accept-Encoding
Content-Length: 8
Content-Type: text/html
Set-Cookie: PHPSESSID=c9bfcf7ddd3fff9d0a05b34541fbf0a9; expires=Wed, 24-Jul-2013 16:24:01 GMT; path=/
code 105
Try to remove this line:
curl_setopt($ch, CURLOPT_HEADER, 1);
Related
I've tried to send a CSV File to eBay FileExchange Service.
I'm writing an application to update a lot of products on eBay at the same time.
When I upload the test.csv by using the eBay CSV-Manager the update will be success, but with the script nothing will happens after post the data.
I've treid the following steps:
Create a separate token for FileExchange.
https://signin.ebay.de/ws/eBayISAPI.dll?SignIn&runame=F-FILEEXL51P1EHH6L899Q9B969GE134DK-FileUpload
Then I use the following script:
$token = 'AgAAAA**AQAAAA**aAAAAA************';
$ebay_url = 'https://bulksell.ebay.com/ws/eBayISAPI.dll?FileExchangeUpload';
$sendheaders = array(
'User-Agent: My Client App v1.0'
);
$fields = array(
'token' => $token,
'file' => '#test.csv'
);
$ch = curl_init($ebay_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 1); // set to 0 to eliminate header info from response
curl_setopt($ch, CURLOPT_NOBODY, 0); // set to 1 to eliminate body info from response
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); // use HTTP/1.0 instead of 1.1
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Returns response data instead of TRUE(1)
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // uncomment this line if you get no gateway response. ###
curl_setopt($ch, CURLOPT_HTTPHEADER, $sendheaders);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); // use HTTP POST to send form data
$resp = curl_exec($ch); //execute post and get results
if(!curl_exec($ch)) {
die('Error: ' . curl_error($ch) . ' - Code: ' . curl_errno($ch));
}
curl_close ($ch);
I've used this csv File-format (test.csv)
Action;ItemID;DispatchTimeMax
Revise;28*********916;30
The results after post:
print_r($resp);
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Set-Cookie: dp1=bu1p/QEBfX0BAX19AQA**617d3da3^bl/DE617d3da3^; Domain=.ebay.com; Expires=Sat, 30-Oct-2021 12:42:11 GMT; Path=/
Set-Cookie: s=CgAD4ACBdvCgjMjFkNjZkMDcxNmUwYTBmMTc1MTA0ZmEwZmZmYjEyZWFY39RE; Domain=.ebay.com; Path=/
Set-Cookie: nonsession=CgADKACBhfT2jMjFkNjZkMDcxNmUwYTBmMTc1MTA0ZmEwZmZmYjEyZWIAywABXbrdqzHTwXKU; Domain=.ebay.com; Expires=Sat, 30-Oct-2021 12:42:11 GMT; Path=/
Cache-Control: private
Pragma: no-cache
Content-Type: text/html;charset=UTF-8
Content-Length: 731
Date: Thu, 31 Oct 2019 12:42:11 GMT
Connection: keep-alive
File upload successful. Your ref # is .
Close
Thanks for helping me.
I have found a solution:
Obviously at php 7.1 the # has no effect, and the file post ist empty to ebay.
I use the curl_file_create function and it's work.
if (!function_exists('curl_file_create'))
{
function curl_file_create($filename, $mimetype = '')
{
return "#$filename;filename="
. ($mimetype ? ";type=$mimetype" : '');
}
}
$fields = array(
"token" => $token,
"file" => curl_file_create ($_GET['filename'], 'text/csv')
);
Hope that help's anybody.
I am trying to get the access token using curl.
$tokenurl = "https://graph.facebook.com/oauth/access_token?client_id=11111111111111&redirect_uri="http://www.example.com/?callback"&client_secret=11111111111111111111111111111&code=" . $_GET['code'];
$_h = curl_init();
curl_setopt($_h, CURLOPT_HEADER, 1);
curl_setopt($_h, CURLOPT_RETURNTRANSFER, true);
curl_setopt($_h, CURLOPT_HTTPGET, 1);
curl_setopt($_h, CURLOPT_URL, $tokenurl );
curl_setopt($_h, CURLOPT_DNS_USE_GLOBAL_CACHE, false );
curl_setopt($_h, CURLOPT_DNS_CACHE_TIMEOUT, 2 );
//var_dump(curl_exec($_h));
$result = curl_exec($_h);
$json = json_decode($result, true);
echo $json['access_token'];
curl_close($_h);
return $result;
var_dump is showing valid data, but I can't fetch it using json_decode(). echo $json['access_token']; shows nothing. Am I doing something wrong? Any suggestion would be helpful.
var_dump is showing this data:
string(733) "HTTP/1.1 200 OK Access-Control-Allow-Origin: * Pragma: no-cache Cache-Control: private, no-cache, no-store, must-revalidate x-fb-rev: 3044559 Content-Type: application/json; charset=UTF-8 x-fb-trace-id: E9niQhqkk34 facebook-api-version: v2.3 Expires: Sat, 01 Jan 2000 00:00:00 GMT Vary: Accept-Encoding X-FB-Debug: 8y64Y0AJvl8YiFFk+kQj8pVvJHBQJLBPC854l5J7e41ypLTYesLTYvfMjsc+FjH9mpZw4Fi7ZITONGM8sazlCw== Date: Thu, 25 May 2017 10:01:35 GMT Transfer-Encoding: chunked Connection: keep-alive {"access_token":"EAAN12SVnRf8BAGGcZBFhAn8Pz7JHqsLVSC00pkZA81ap4rZAJOwSl3ZABaQPT7L03vPSNnsGS2lTduSN1FQMy1q8vRqiefD0sCd3sN4wu1n9tuMgMqXCeVi5zAKod1oPrjgJA246VHN5qkOxQGZBj52ZCgWblJ877cZD","token_type":"bearer","expires_in":5118394}" 0
var_dump is showing valid data, but I can't fetch it using json_decode(). echo $json['access_token']; shows nothing. Am I doing something wrong?
Yes - you are trying to decode the whole response, headers and body, as JSON.
Only the body contains the JSON data you are interested in - so don't set CURLOPT_HEADER.
In my first cURL request i upload a file and i set a $_SESSION variable with the name, extension etc. In my second cURL request i want to move the uploaded file from tmp folder to user folder but baddly the $_SESSION variable is empty. why?
first request code code looks like this:
$upload = curl_init();
curl_setopt($upload, CURLOPT_URL, "http://localhost/upload/" );
curl_setopt($upload, CURLOPT_POST, true );
curl_setopt($upload, CURLOPT_RETURNTRANSFER, true );
curl_setopt($upload, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"] );
curl_setopt($upload, CURLOPT_HTTPHEADER, array("Set-Cookie: data=" . urldecode($cookie) ));
curl_custom_postfields($upload, $fields, $files);
$res = curl_exec($upload);
curl_close($upload);
and the second request code: following the first request:
$submit = curl_init();
curl_setopt($submit, CURLOPT_URL, "http://localhost/" );
curl_setopt($submit, CURLOPT_RETURNTRANSFER, true );
curl_setopt($submit, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"] );
curl_setopt($submit, CURLOPT_POST, count($fields));
curl_setopt($submit, CURLOPT_POSTFIELDS, $fields_string );
curl_setopt($submit, CURLOPT_HEADER, true);
curl_setopt($submit, CURLOPT_HTTPHEADER, array("Cookie: data=" . urldecode($cookie) ));
$res = curl_exec($submit);
curl_close($submit);
is there any option to keep session alive? is the same problem i meet on AJAX requests when i start using javascript with AJAX i think.
my response header:
HTTP/1.1 200 OK
Date: Wed, 04 Mar 2015 02:24:22 GMT
Server: Apache/2.2.12 (Win32) DAV/2 mod_ssl/2.2.12 OpenSSL/0.9.8k mod_autoindex_color PHP/5.3.0 mod_perl/2.0.4 Perl/v5.10.0
X-Powered-By: PHP/5.3.0
Set-Cookie: PHPSESSID=g7hc328ij8lr63mps6ub44gat2; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Length: 22
Content-Type: text/html
I think you need a cookie jar to keep track of your session:
curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/cookies");
I am here with a lot of hope to solve my problem. I have this website i want to login to and access a member only page. so basically php code with cURL to login and then open a member only page. Below is the code i used. But each time i try to access member only page, it asks me to login. So it does not save in the session or cookie that i already logged in.
..........
set_time_limit(0);
$username = 'myemail#yahoo.com';
$password = 'mypassword';
$loginUrl = 'http://radaris.com/login/a.login';
$cookie_file_path = getcwd() . '/tmp/cookie.txt';
//init curl
$ch = curl_init();
//Set the URL to work with
curl_setopt($ch, CURLOPT_URL, $loginUrl);
// ENABLE HTTP POST
curl_setopt($ch, CURLOPT_POST, 0);
//Set the post parameters
curl_setopt($ch, CURLOPT_HEADER, 1); // set to 0 to eliminate header info from response
curl_setopt($ch, CURLOPT_POSTFIELDS, 'email='.$username.'&password='.$password."&remember_me=1");
//Handle cookies for the login
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
//Setting CURLOPT_RETURNTRANSFER variable to 1 will force cURL
//not to print out the results of its query.
//Instead, it will return the results as a string return value
//from curl_exec() instead of the usual true/false.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//execute the request (the login)
$store = curl_exec($ch);
echo "<font color=red>Login</font><br>".$store."<br><br>";
$postfields = array();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://radaris.com/my/');
curl_setopt($ch, CURLOPT_HEADER, 1); // set to 0 to eliminate header info from response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, 1);
//Handle cookies for the login
// Edit: prior variable $postFields should be $postfields;
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$result = curl_exec($ch);
echo "<font color=red>Report</font><br>".$result."<br><br>";
..........
The Result i get is :
Login HTTP/1.1 200 OK Server: nginx Date: Thu, 12 Jun 2014 14:21:01
GMT Content-Type: application/json Transfer-Encoding: chunked
Connection: keep-alive Keep-Alive: timeout=20 Cache-Control: no-store,
no-cache, must-revalidate Cache-Control: post-check=0, pre-check=0
Expires: Wed, 14 Jan 2009 19:00:00 GMT X-Robots-Tag: noindex
{"/Rdf.showError":[["Invalid email or password","login_form"],["Check
your email","login_email"],["Check your password","login_password"]]}
Report HTTP/1.1 302 Found Server: nginx Date: Thu, 12 Jun 2014
14:21:03 GMT Content-Type: text/html; charset=utf-8 Transfer-Encoding:
chunked Connection: keep-alive Keep-Alive: timeout=20 Location:
/login?backurl=%2Fmy%2F HTTP/1.1 200 OK Server: nginx Date: Thu, 12
Jun 2014 14:21:03 GMT Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked Connection: keep-alive Keep-Alive:
timeout=20 Vary: Accept-Encoding Vary: Accept-Encoding Set-Cookie:
PHPSESSID=qdg80d48dd17hl879h86v9ruo5; path=/; HttpOnly Expires: Thu,
19 Nov 1981 08:52:00 GMT Cache-Control: no-store, no-cache,
must-revalidate, post-check=0, pre-check=0 Pragma: no-cache Login We
never share or disclose your information!
The above are the headers only. in the 2nd header i get the login form again to login meaning the successful login in the first bit of my code did not save the session that is why when i access member only page, it asks to login.
I am really desperate to get this done right. Thanks everyone in advance.
You're using two separate curl handles, and you never set up the cookie stuff in the second one. Each curl handle is completely independent of each other, so if you don't tell the second handle about the cookiejar/cookiefile you created in the first one, you never actually use the cookie you got from the login request.
Technically, you do NOT have to do one-request-per-handle. A single handle can do multiple requests. The only time it might be easier to start a new one is if you made a complicated custom request with many options set, and it's easier to just start fresh rather than reset those options for the next request.
So your code sequence should be:
init curl
set up cookiefile/cookiefar and other options
do login request
do next next request
close curl
If you don't want to rewrite, then at least add the cookiejar/cookiefile options to the second request.
i am trying to download a file using box.net using API in php.
As per the documentation i wrote up the code.
but in response i am getting some strange texts.
here's my code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.box.com/2.0/files/3934139624/content ");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPGET,true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: BoxAuth api_key={MyApikey}&auth_token={Mytoken}"));
$result = curl_exec($ch);
die('DIE');
I am getting response something like this:
PK!Ðòš-[Content_Types].xml ¢( ´UËNÃ0¼#ñ‘¯¨qË!Ô´G¨Dù×Þ´‰mÙÛ×ß³IšA›ˆ†^"EÑÎÌÎÎnÆÓ]žEðA[“°QH¯~ÉŸb üv8¼ãÒƒ,0ØdüF¼VÍ„ÇW‘ßZ¯xj-‹b‚cÑcUWP'L8—i)„óQ?H6Mµeå:'ª¸€sÞJZ˳¸¾) ùdü©Xg=ïH[e‡‡,üõÐfL•¥²°Ò.´0´·uPvÒž¦»v˜3Üis¡Mÿ¤³ÎàÉ×ÿSÝ)"à>»DP*ÜNz0êBI‘Û$мfÞºÀi+zŠ P ´0"f3°£\…ȾTºI S‘ÌõŒ«º¾ÇôWš™¦ÚY igï#µÇX6_Ö]7~ fïØˉÈaoÙ.b*lIÆrj)õ,l0Ï%‘b¬ 6ài¢ÕõDÿ_‹Ž…, ¡ ‰Ïó|uœZ^tÙ¢yǯ;!Y,}{ûCƒ³/h>ÿÿPK!¿hJä1>word/rels/document.xml.rels ¢( ¬”ËNÃ0E÷HüCä=qR q'æ>¾ƒ“‘ˆsµà©WÃ-ŽÌEî›nâ>ðÍqã¨Í§y±3ÆóüükeìE±ty’àÕ³üÍ黦ÏÖ¤KLÏhóÊŸi¾IàˆpzÒŽ¹ç?}xÛxx;ùgïÐ¥f7Yô KéMèwÄÆÇÐEïúÃF§³ß9ètÏ7ÌKWxÐ/žñ¡“ùéâ;W…—Ô•¯bú%B×óù§ìv îã㡈“ô£ 8ÜílìÐqq~x|!Ã4Á1Nâ ñaãVš+¾•ËÓr¤ØLe'õc"ójS“Œ(ñR'»>wbriê’6œ,•ôçPøH†.ÔO«<çµ¼G›[¯ ‹Ÿ~ëÈŒcñ)“ )ò<4/nÌ—ôEÛþßpÄÙ÷æ¬Û?xg«\ÖîЃSäÀ•Ç°tÒ(¾‹³ƒwïg˜³ÕKøŒ;ù¾.†ì, l©´ªµÐm¯]‰ŠTíßnÁ¿·ß¤/ë»–ª”짓6õ“^Qð-wô—Qð]6bé à²#ÆûÍ#¡™˜×Fa'™Â†êMî'ÂÛ¿U*XÆÞ/¾\ÁÜl X5HñKÕ˜sØ8EÌ/!вÃÐeq”µ±dº¨É…⛂R—7ЊU¹iØF:h±FÎç¢àõð¾ôÈ!˜&æ',ADSÈP¸L‘M.úìäpow½Ý(¥Ú·R ãpK0è7^;¿Lë4f¤P3Ì…#M s´ï¡Ü:…(#à(1ß;9|÷S½°T4ϹF²ì%“$åÁf“tÖة⼪R&˜nn†)#éóÒfŽBC?‰ð€()ÄÝ(%LNËñ)V^«ÞÛ¶[5+Í>jÀ£WlŽ÷¡¿)ÁoôFBû›CÕ©ëÜ™ÖI¦æÖQ×ƤHX-ijž^ÀDûs“ …Ø}
Can any one tell me how can i handle such kind of response?
thanks in advance.
As per box.net api documentation:
The response to this request will simply be the complete data of the
file itself.
So all you need to save file content locally.
In response header, you need to check content-type, right now it is XML
$result = curl_exec($ch);
$fp = fopen('test.xml','wb');
fwrite($fp, $result);
fclose($fp);
#GBD following comes in response header:
HTTP/1.1 302 Found
Server: nginx
Date: Wed, 14 Nov 2012 09:11:51 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
Cache-control: private
Location: https://dl.boxcloud.com/bc/1/85f471520cf611a05025a5f/JolueqOGpciD6dgYhecNBoVpYxkvmYe1ZLheZor6BF4DUBIelMQTkFwYIys3nIibNIIEHUp447tBZLaXDzIbNQ,,/a44510a2b21219463fade41d6b36dabf/
Content-Length: 0
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 14 Nov 2012 09:11:52 GMT
Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document
Content-Length: 19944
Connection: keep-alive
Cache-control: private
Accept-Ranges: bytes
Content-Disposition: attachment;filename="cloud computing proposal.docx";filename*=UTF-8''cloud%20computing%20proposal.docx
X-Content-Type-Options: nosniff
Accept-Ranges: bytes
And saving file in xml also,couldn't b opened.