Jump to content

Google image search in PHP - help!


Recommended Posts

So I have the following PHP script which works well. 

 

I'm a newbie here, but trying to create a workflow where I can enter an argument, and it pings google, and returns the top result URL to me, copied to my clipboard. 

 

Like I said, on the last line, I do successfully return the top result, but not entirely sure how to hook up the rest of the workflow.

 

Here you can see the workflow at the moment -> https://www.dropbox.com/s/pfo7b0uap41bd0g/Screenshot%202014-08-05%2016.13.02.png

 

Any help would be much appreciated! 

 

function get_url_contents($url) {
    $crl = curl_init();


    curl_setopt($crl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)');
    curl_setopt($crl, CURLOPT_URL, $url);
    curl_setopt($crl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($crl, CURLOPT_CONNECTTIMEOUT, 5);


    $ret = curl_exec($crl);
    curl_close($crl);
    return $ret;
}


$q = $argv[1]; //Get user-inputted argument


$json = get_url_contents('http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=.$q.');


$data = json_decode($json);


foreach ($data->responseData->results as $result) {
    $results[] = array('url' => $result->url, 'alt' => $result->title);
}


$url = $results[0]['url'];


echo $url;
Edited by jp888
Link to comment

Are you sure that script works?

 

I think that the argument in 

$json = get_url_contents( ...

 needs to have double quotes rather than single quotes. Otherwise, the $q variable won't be expanded.

 

 

That being said:

 

The easiest way to hook this up would be to hook this up to a keyword with an argument required. Then, connect that with a "run script" type PHP, and paste what you have into the script box. However, change

$q = $argv[1];

to

$q = "{query}"

Lastly, connect a "Copy to Clipboard" output after the run script. Make sure the copy clipboard box has "{query}" in it. Then, it should work.

Link to comment

Not too hard:

  • Save the PHP code to a file called something. For now, we'll just call it "script.php"
  • Change the run script from PHP to Bash.
  • Change the contents of the Run script to "php script.php"
  • Add in a line to the Run script that is "php mynextscript.php"

But, it depends more on what you're trying to do. Are you trying to modify the output from the first script? Are you trying to download an image? It might be easier to write it all as one script.

 

Btw: save the new files into the Workflow directory. The easiest way to find that is to right-click on the workflow in the sidebar and select "Show in Finder."

Edited by Shawn Rice
Link to comment

So, here is the script that you'll need:

<?php
// alfred_to_hipchat.php

require_once( '/Users/james/hipchat/src/HipChat/HipChat.php' );

if ( ! isset( $argv[1] ) || empty( $argv[1] ) ) {
	die( "Error: needs to use an argument" );
}

$q = $argv[1];
$json = get_url_contents( "http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=.{$q}." );

$data = json_decode($json);

foreach ($data->responseData->results as $result) {
    $results[] = array('url' => $result->url, 'alt' => $result->title);
}

$url = $results[0][ 'url' ];

// Some nascent error handling
if ( $url ) {
	echo $url;
	post_to_hc( $url )
} else {
	die( "Could not get URL for query '$q'" );
}

function get_url_contents( $url ) {
    $crl = curl_init();

    curl_setopt($crl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)' );
    curl_setopt($crl, CURLOPT_URL, $url );
    curl_setopt($crl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($crl, CURLOPT_CONNECTTIMEOUT, 5);

    // Some nascent error handling
    if ( ( $ret = curl_exec($crl) ) === FALSE ) {
	    curl_close( $crl );
	    exit(1);
	} else {
	    curl_close( $crl );
	    return $ret;
	}   
}
 
function post_to_hc( $url ) {
	$token = 'my_token_here';
	$hc = new HipChat\HipChat( $token );
	$hc->message_room('SpartaHQ', 'Sparta_Bot', $url );
}

It adds simple error handling, which is a good thing so that you don't paste weird stuff into Hipchat with malformed URLs.

 

One thing to remember — and this is really important — is that PHP doesn't do variable expansion in single quotes. So

<?php

$var = 'My var';

echo '$var'; // Outputs, literally, "$var"
echo "$var"; // Outputs "My var"

So, I took those out of the scripts.

 

For the workflow:

 

Have the Run Script object be "Bash" and the contents should simply be

out=$(php alfred_to_hipchat.php "{query}")
if [[ $? -eq 0 ]]; then
  echo "${out}"
else
  echo "Error getting URL for '{query}'" >&2
fi

Next, in the clipboard action, check the box that says to ignore it if {query} is empty.

 

If you're having trouble often with this, then open the debugger in the workflow and run the script. You should see some information printed in red when the script exits with die.

 

The bash code is just redundant error checking that will print the URL if the PHP script exits with a status code 0 (indicating success). The `$?` variable is just the exit code of the previous command, which, in this case, is the php command to run the script.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...