How to use zend_http_client or curl in right way - php

I'm using zend_http_client. What i want is to send a request to transfer(without redirecting) to another site and send number in field that I have and get the answer with info i need to use on my site. That's what i'm doing, but how to send my number to that field and submit it?
$url = 'http://gdeposylka.ru/';
$config = array(
'timeout' => 30
);
$client = new Zend_Http_Client($url, $config);
try {
$response = $client->request('GET');
if ($response->getStatus() == 200) {
$ctype = $response->getHeader('Content-type');
$body = $response->getBody();
$dom = new Zend_Dom_Query($body);
$results = $dom->query('limit');
$item->site_k = 1 + (int) $results->current()->textContent;
var_dump($response);
exit;
}
} catch (Zend_Http_Client_Adapter_Exception $e) {
}
Thanks and sorry for grammar mistakes.

Because there is a hash on the site you're trying to send a request, you probably wont be able to get this done. Hash mechanism makes it impossible to send request straight to the URL stated in action param of form tag. It's designed particulary to prevent such action you want to make. Still, since there is no captcha you can try web automation.
What you might find useful is ie. Selenium http://docs.seleniumhq.org/

Related

What does it mean if your request from postman returns HTML body?

When I send a request in postman, it returns a html body and it says 404 not found. Does this mean my php server is not running? I am trying to run my php code in visual studio code as a php server.
This is the php file:
<?php
// Headers
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
include_once '../../config/Database.php';
include_once '../../models/Login.php';
// Instantiate DB & connect
$database = new Database();
$db = $database->connect();
// Instantiate blog post object
$login = new Login($db);
//$login->email=isset($_GET['username']) ? $_GET['username']:die();
$login->password=isset($_GET['phoneNumber']) ? $_GET['phoneNumber']:die();
$result = $login->read_single();
$num = $result->rowCount();
if($num > 0) {
// Post array
$posts_arr = array();
$posts_arr['data2'] = array();
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
extract($row);
$post_item = array(
//'username' => $username,
'phoneNumber' => $phoneNumber,
'token' =>$token,
'tokencreatedat'=>$tokencreatedat,
'expTime'=>$expTime
);
// Push to "data"
//array_push($posts_arr, $post_item);
array_push($posts_arr['data2'], $post_item);
}
// Turn to JSON & output
echo json_encode($posts_arr);
} else {
// No Posts
echo json_encode(
array('message' => 'No Posts Found')
);
}
?>
Plus, how do you run php as a web server for RESTful API?
UPDATE: when I put localhost:3000/path in the url in postman, it return 200 which mean its okay. But when I input the IP address, it returns 404.
404 means the url you are trying to get is not found !
as per your html,I think your api endpoints ( if you created any or not in both case) has an issue so its showing 404.
I assume you are trying to create an api for user login right ?
at first you should learn about the RESTful Api concept and how to create api endpoints.
this might help
For a quick learning:
create a login_api.php file which will contain php code only. it will take your username / password etc from _POST[], validate your user and redirect you to your home.php
This means that the api which you are hitting have some issues.
So in your case most probably you have some issues with your php laravel code, that's why whenever you are calling that api then html code was returned on behalf of that.
Moreover 404 means that api is not found but then your service is running
Please check your code for syntactical bugs.
For Restful Api's using php, you can refer this and this

Yii loses session after OAuth redirect

I've lost a day and a half now trying to figure out why Yii is deleting all of the session data after I go to Twitter's OAuth page and back to my redirect.
Here is the main SiteController, where I go to Twitter. Here I am trying to save the oauth_token and token_secret values, so I can use them on the redirect controller.
function actionTwitter()
{
$consumerKey = "";
$consumerSecret = "";
$connection = new TwitterOAuth($consumerKey, $consumerSecret);
$request_token = $connection->oauth("oauth/request_token", array("oauth_callback" => "http://127.0.0.1/yii/?r=redirect&type=twitter"));
$oauth_token=$request_token['oauth_token'];
$token_secret=$request_token['oauth_token_secret'];
Yii::app()->session['token'] = $oauth_token; // This doesn't save!!
Yii::app()->session['token_secret'] = $token_secret; // This does not save!!
$url = $connection->url("oauth/authorize", array("oauth_token" => $oauth_token));
$this->redirect($url);
exit(); // some people have said I need to exit the session first after I redirect, but it doesn't help at all.
}
Here is my RedirectController, which is a separate controller and not in the main SiteController:
public function actionIndex()
{
$type = $_GET['type'];
if ($type == "twitter")
{
$token = Yii::app()->session['token'];
print($token);
}
}
I also have the session autostart set to true in my config file.
Thoughts on why it isn't working / stuff I have read about:
Twitter's site is HTTPS, and I am on localhost (which isn't HTTPS). For some reason that I forget this will make the session lose data when I redirect. If this is the case, how do I fix it without using HTTPS?
When I create new CHttpCookies they do not save either, I can't retrieve the value
I have tried Yii::app()->user->setState instead, which isn't working either.
I found the solution. It did not work because I was using 127.0.0.1 for the redirect, instead of the standard localhost. I changed that and all is working now.

How to handle twilio outgoing call if no answer

I am implementing twilio outgoing call with wordpress woocommerce. As soon as the order is placed the site owner will receive a call. However i couldn't find a way to handle if the person didn't answer the call for some reason. The documentation is going above my head.
What i want is to make twilio recall if the call isn't answered. Or i am open to suggestion how else would be the good way to handle. Please note that i have not created any application at Twilio yet. Just using the PHP SDK as the account id and token is provided by default.
here is the code
function send_order_Call($order_id) {
try {
$file = plugin_dir_path(__FILE__) . "order.xml";
$twiML = simplexml_load_file($file);
$twiML->Say = "Hello, You have received a new order. The order id is {$order_id}. Kindly check you fax for details";
file_put_contents($file, $twiML->asXML());
require_once 'Twilio.php';
//Initializing Twilio Rest
$sid = "ACcbd06f8e73asdfsdaf1";
$token = "32ccf4bdcasdfsafc";
$client = new Services_Twilio($sid, $token);
$call = $client->account->calls->create("+12asfsaf", "+1ssd8777asfsf7", site_url()."/wp-content/plugins/woocommerce-twilio/order.xml", array());
// echo $call->sid;
} catch (Exception $e){
$error = $e->getMessage();
die($error);
}
}
TL;DR; You need to set the statusCallBack parameter for the options array.
Please see https://stackoverflow.com/a/24482140/1751451

Reddit API in php returns bad captcha for submitting story

Using php for Reddit api for submitting a story returns bad captcha as error.
I am able to login using the api and get usermod and captcha perfectly using api.
Ideally if the reddit_session cookie is passed it should post and not return bad captcha can someone shed me some light on this..
reference link:
https://github.com/reddit/reddit/wiki/API
<?php
$user = "";
$passwd = "";
$url = "http://www.reddit.com/api/login/".$user;
$r = new HttpRequest($url, HttpRequest::METH_POST);
$r->addPostFields(array('api_type' => 'json', 'user' => $user, 'passwd' => $passwd));
try {
$send = $r->send();
$userinfo = $send->getBody();
} catch (HttpException $ex) {
echo $ex;
}
$arr = json_decode($userinfo,true);
$modhash = $arr['json']['data']['modhash'];
$reddit_session = $arr['json']['data']['cookie'];
$post = array('uh'=>$modhash,
'kind'=>'link',
'url'=>'yourlink.com',
'sr'=>'funny',
'title'=>'omog-asdfasf',
'id'=>'newlink',
'r'=>'funnyier',
'renderstyle'=> 'html'
);
$url = "http://www.reddit.com/api/submit";
// Upvote RoboHobo's comment :)
// Add user cookie data
$r->addCookies(array("reddit_session" => $reddit_session));
// Set URL to vote
$r->setUrl($url);
// Add vote information, found at http://wiki.github.com/talklittle/reddit-is-fun/api-all-functions
$r->setPostFields($post);
// Send request blindly
try {
$userinfo = $r->send();
} catch (HttpException $ex) {
echo $ex;
}
pre($userinfo);
exit;
function pre($r){
echo "<pre />";
print_r($r);
}
?>
For anyone else that stumbled onto this question lately and is still having that issue:
The above issue was fixed and works correctly however if you created a new account for your reddit bot and try to submit a story you will recieve a bad_captcha error. New accounts have to submit captchas until they gain a certain amount of karma so this is the error you are seeing. Try the request with an older account and this should solve your problem.
From what I can tell, at the moment CAPTCHA is broken in the Reddit API. They originally were using an outdated PyCAPTCHA and were migrating to reCAPTCHA. Since then, there has been an issue with using api_type:json which has a work around and someone on github is currently working it. He also offered an explanation/solution:
Quite simply, the json (though not jquery) result should contain the captcha_id when a >captcha is required. By captcha_id I mean the part to complete a url like the followng: >http://www.reddit.com/captcha/(captcha_id).png
The use case I encountered is when trying to submit a story via the api using >api_type:json. I am nicely notified that my non-existent captcha is incorrect, however, I >then have to make a request to http://www.reddit.com/api/new_captcha in order to get the >captcha_id. This last round trip seems unnecessary.

Submitting a form after processing in PHP

we have a form that grabs a whole bunch of details and then submits it to a mail program which send an email to certain people.
I need to put some extra processing between the form and mail program which checks info from a db and then changes the form data accordingly.
I thought I would be able to use curl for this but once I do the processing I need to submit the form and have things run the way they would as if the php wasn't there and it seems I can't do this with curl.
Question is can I do this with curl? Or is there a better way to go about this. (although I can do minor changes to both the mail and form they should stay the same as much as possible).
Edit: I'm not sure what information I can put in but I'll try and simplify it.
At the moment: form -> mail program
What I am trying to do: form -> php ->mail program
Mail program takes POST variables so what I want to do is grab the post variables, change some of them and then send them to the mail program and the previous process looks the same.
Use this:
http://wezfurlong.org/blog/2006/nov/http-post-from-php-without-curl/
<?php
function do_post_request($url, $data, $optional_headers = null)
{
$params = array('http' => array(
'method' => 'POST',
'content' => $data
));
if ($optional_headers !== null) {
$params['http']['header'] = $optional_headers;
}
$ctx = stream_context_create($params);
$fp = #fopen($url, 'rb', false, $ctx);
if (!$fp) {
throw new Exception("Problem with $url, $php_errormsg");
}
$response = #stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from $url, $php_errormsg");
}
return $response;
}
Requires PHP5. Arrange all the data you need in PHP and stream out the post values.
make the PHP file to generate a form with the attribute filled with processed value. then you use Javascript to submit the form in the end.
Short answer was curl can do it for what I need, needed:
`curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);`

Categories